0

感谢您查看我的代码。我正在学习 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();

    }
};
4

7 回答 7

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();
}
于 2013-09-30T04:33:04.900 回答
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.
}
于 2013-09-30T04:42:13.357 回答
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();

    }
}
于 2013-09-30T04:58:31.473 回答
0

你也应该把扫描仪放在里面,同时:

 do {
 Scanner stdin = new Scanner(System.in);
            if (stdin.nextInt() % 2 == 0)
                sumP += stdin.nextInt();
            else
                sumO += stdin.nextInt();
        }while ((stdin.nextInt()) >= 0)
于 2013-09-30T04:30:05.213 回答
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
}
于 2013-09-30T04:34:58.423 回答
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);

    }

}

};

于 2013-09-30T04:42:47.887 回答
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() 不会减少它只会返回您的 stdin 的值,这就是它无法正确循环的原因。

于 2013-09-30T05:53:46.897 回答