如何在java中添加while循环?

时间:2013-09-30 04:26:15

标签: 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, 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();

    }
};

7 个答案:

答案 0 :(得分:3)

每次调用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();
}

答案 1 :(得分:1)

我认为这解决了这个问题,

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.
}

答案 2 :(得分:1)

问题是你每次都在使用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();

    }
}

答案 3 :(得分:0)

你应该把扫描仪放在里面:

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

答案 4 :(得分:0)

while ((stdin.nextInt()) >= 0) {
    if (stdin.nextInt() % 2 == 0)
       sumP += stdin.nextInt();
    else
       sumO += stdin.nextInt();
}

你的问题是你每次循环时都要读3个数字。存储您的阅读结果,然后决定如何处理它,不要丢弃它并再读2个数字。

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

答案 5 :(得分:0)

我想你想要像这样编码

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);

    }

}

};

答案 6 :(得分:0)

请尝试这些代码

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()不会减少它只返回你的标准输入的值,这就是它没有正确循环的原因。