Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中添加while循环?_Java_While Loop - Fatal编程技术网

如何在java中添加while循环?

如何在java中添加while循环?,java,while-loop,Java,While Loop,谢谢你看我的代码。 我正在学习java,遇到了一个让我发疯的问题 /* * The loop reads positive integers from standard input and that * terminates when it reads an integer that is not positive. After the loop * terminates, it prints out, separated by a space and on a single line,

谢谢你看我的代码。 我正在学习java,遇到了一个让我发疯的问题

/*
 * The loop reads positive integers from standard input and that
 * terminates when it reads an integer that is not positive. After the loop
 * terminates, it prints out, separated by a space and on a single line, the
 * sum of all the even integers read and the sum of all the odd integers
 */
问题是变量没有添加!我知道我的语法很好。我认为java语言中有一些东西我不理解循环是如何工作和添加的

import java.util.Scanner;

class Testing2 {
    public static void main(String[] args) {
        int sumP = 0;
        int sumO = 0;

        Scanner stdin = new Scanner(System.in);

        System.out.println("Enter a positive or negative integer: ");

        while ((stdin.nextInt()) >= 0) {
            if (stdin.nextInt() % 2 == 0)
                sumP += stdin.nextInt();
            else
                sumO += stdin.nextInt();
        }
        System.out.println(sumP + " " + sumO);
        stdin.close();

    }
};

u还应将扫描仪放在内部,同时:

 do {
 Scanner stdin = new Scanner(System.in);
            if (stdin.nextInt() % 2 == 0)
                sumP += stdin.nextInt();
            else
                sumO += stdin.nextInt();
        }while ((stdin.nextInt()) >= 0)

u还应将扫描仪放在内部,同时:

 do {
 Scanner stdin = new Scanner(System.in);
            if (stdin.nextInt() % 2 == 0)
                sumP += stdin.nextInt();
            else
                sumO += stdin.nextInt();
        }while ((stdin.nextInt()) >= 0)

每次调用stdin.nextInt()时,它都在寻找另一个整数。为了避免这种情况,在顶部设置一个等于输入的变量:

int myInt = stdin.nextInt();

if (myInt >= 0) {
 if (myInt % 2 == 0)
                sumP += myInt;
            else
                sumO += myInt;
        }
        System.out.println(sumP + " " + sumO);
        stdin.close();

    }
}
也不要在最后一个大括号后加分号。如果您希望输入多个数字,您可以使用

while(stdin.hasNext()){
int myInt = stdin.nextInt();
}

每次调用stdin.nextInt()时,它都在寻找另一个整数。为了避免这种情况,在顶部设置一个等于输入的变量:

int myInt = stdin.nextInt();

if (myInt >= 0) {
 if (myInt % 2 == 0)
                sumP += myInt;
            else
                sumO += myInt;
        }
        System.out.println(sumP + " " + sumO);
        stdin.close();

    }
}
也不要在最后一个大括号后加分号。如果您希望输入多个数字,您可以使用

while(stdin.hasNext()){
int myInt = stdin.nextInt();
}
你的问题是每次循环时你都在读3个数字。存储读取结果,然后决定如何处理它,不要放弃它,再读取2个数字

int nextInt;
while ((nextInt = stdin.nextInt()) >= 0) {
    // Do things with nextInt
}
你的问题是每次循环时你都在读3个数字。存储读取结果,然后决定如何处理它,不要放弃它,再读取2个数字

int nextInt;
while ((nextInt = stdin.nextInt()) >= 0) {
    // Do things with nextInt
}

我认为这解决了问题

Scanner stdin = new Scanner(System.in);
while(1)
{
int num = stdin.nextInt();
if(num<0)
{
     stdin.close();
     break;
}
else
{
  if(num%2==0)
  {
  //Initialize sumP and sumO to 0
  sumP=sumP+num;
  }
  else
  {
  sumO=sumO+num;
  }
}
//You can now output sumP and sum) outside the loop safely.
}
Scanner stdin=新扫描仪(System.in);
而(1)
{
int num=stdin.nextInt();

如果(num我认为这解决了问题

Scanner stdin = new Scanner(System.in);
while(1)
{
int num = stdin.nextInt();
if(num<0)
{
     stdin.close();
     break;
}
else
{
  if(num%2==0)
  {
  //Initialize sumP and sumO to 0
  sumP=sumP+num;
  }
  else
  {
  sumO=sumO+num;
  }
}
//You can now output sumP and sum) outside the loop safely.
}
Scanner stdin=新扫描仪(System.in);
而(1)
{
int num=stdin.nextInt();

如果(num我认为您需要这样的编码

import java.util.Scanner;

class TestScaner {
public static void main(String[] args) {
    int sumP = 0;
    int sumO = 0;
    Scanner stdin = null;
    while (true) {
        stdin = new Scanner(System.in);

        System.out.println("Enter a positive or negative integer: ");

        int num = stdin.nextInt();

        if (num % 2 == 0)
            sumP += num;
        else
            sumO += num;

        System.out.println("==" + sumP + " " + sumO);

    }

}

}我想你需要这样的编码

import java.util.Scanner;

class TestScaner {
public static void main(String[] args) {
    int sumP = 0;
    int sumO = 0;
    Scanner stdin = null;
    while (true) {
        stdin = new Scanner(System.in);

        System.out.println("Enter a positive or negative integer: ");

        int num = stdin.nextInt();

        if (num % 2 == 0)
            sumP += num;
        else
            sumO += num;

        System.out.println("==" + sumP + " " + sumO);

    }

}

}问题是每次都在使用nextInt()。 像这样使用-

import java.util.Scanner;

class Testing2 {
    public static void main(String[] args) {
        int sumP = 0;
        int sumO = 0;

        Scanner stdin = new Scanner(System.in);

        System.out.println("Enter a positive or negative integer: ");
        int temp;
        while ((temp=stdin.nextInt())>0) {
            if (temp % 2 == 0)
                sumP += temp;
            else
                sumO += temp;
        }
        System.out.println(sumP + " " + sumO);
        stdin.close();

    }
}

问题是您每次都在使用nextInt()。 像这样使用-

import java.util.Scanner;

class Testing2 {
    public static void main(String[] args) {
        int sumP = 0;
        int sumO = 0;

        Scanner stdin = new Scanner(System.in);

        System.out.println("Enter a positive or negative integer: ");
        int temp;
        while ((temp=stdin.nextInt())>0) {
            if (temp % 2 == 0)
                sumP += temp;
            else
                sumO += temp;
        }
        System.out.println(sumP + " " + sumO);
        stdin.close();

    }
}
请尝试这些代码

import java.util.Scanner;

class Testing2 {
    public static void main(String[] args) {
    int sumP = 0;
    int sumO = 0;
    int scn = 0;

    Scanner stdin = new Scanner(System.in);

    System.out.println("Enter a positive or negative integer: ");

    scn = stdin.nextInt();
   while (scn >= 0) {

       System.out.println("stdin next " + scn);
        if (scn % 2 == 0){
            sumP += scn;
        }else{
           sumO += scn;
        }
       scn--;
    }
    System.out.println(sumP + " " + sumO);


   }
};
您的stdin.nextInt()不会降低它,只会返回一个stdin值,这就是它不能正常循环的原因。

请尝试这些代码

import java.util.Scanner;

class Testing2 {
    public static void main(String[] args) {
    int sumP = 0;
    int sumO = 0;
    int scn = 0;

    Scanner stdin = new Scanner(System.in);

    System.out.println("Enter a positive or negative integer: ");

    scn = stdin.nextInt();
   while (scn >= 0) {

       System.out.println("stdin next " + scn);
        if (scn % 2 == 0){
            sumP += scn;
        }else{
           sumO += scn;
        }
       scn--;
    }
    System.out.println(sumP + " " + sumO);


   }
};

stdin.nextInt()不会减少它,只返回一个stdin值,这就是它不能正常循环的原因。

确定吗?为什么不添加几个System.out.println()语句来跟踪执行?确定吗?为什么不添加几个System.out.println()呢跟踪执行的语句?我想您是想用
myInt
替换更多的
nextInt()
调用,对吗?是的,很抱歉,编辑它以使用变量而不是stdin.nextInt()import java.util.Scanner;类Testing2{public static void main(String[]args){int-sumP=0;int-sumO=0;System.out.println(“输入一个正整数或负整数:”;Scanner-stdin=new Scanner(System.in);while(stdin.hasNext()){int-myInt=stdin.nextInt();if(myInt>=0){if(myInt%2==0)sumP+=myInt;else-sumO+=myInt;}System.out.println(sumP+“”+sumO)}stdin.close();};谢谢您的时间。您的帮助肯定很有帮助!我想您是想用
myInt
替换更多的
nextInt()
调用,不是吗?是的,很抱歉,将其编辑为使用变量而不是stdin.nextInt()import java.util.Scanner;类测试2{public static void main(String[/args){int-sumP=0;int-sumO=0;System.out.println(“输入一个正整数或负整数:”;Scanner-stdin=new Scanner(System.in);while(stdin.hasNext()){int-myInt=stdin.nextInt();if(myInt>=0){if(myInt%2==0)sumP+=myInt;else-sumO+=myInt;}System.out.println(sumP+“”+sumO);}stdin.close();};谢谢你的时间。你的帮助肯定有帮助!这甚至不会编译。你不能两次定义stdin。这也不是问题的解决方案。是的。我想说的是扫描仪必须放入循环中。这甚至不会编译。你不能两次定义stdin。这也不是问题的解决方案。是的。我想说的是扫描仪必须放入循环中内环。