0

我试图要求用户输入最多 5 个数字,每个数字用空格分隔。

例如

最多输入 5 个数字:3 4 5

我将把它们加到整数和中,然后再除以计数器

得到这些数字的平均值。

但是,我的循环似乎没有结束。我的代码有什么问题?

    int counter = 0 , sum = 0;

    Scanner scan = new Scanner(System.in);

    System.out.println("enter up to 5 numbers");

    while(scan.hasNextInt());{
    counter++;
    sum += scan.nextInt();
    }
    System.out.println(counter);
    System.out.println(sum);
4

6 回答 6

1

You put a ; between while and {, so it loops. Remove it.

于 2013-10-06T02:52:59.940 回答
0

Scanner.hasNextInt() does not do what you seem to think it does. It does not tell you whether there is an integer available in already typed input (it does not have any conception of what has "been typed"), but rather whether the input waiting can be read as an integer. If there is no input already waiting, it will block until there is, so your loop is simply sitting there forever, blocking for more input.

What you probably want to do instead is to read a whole line, and then split it explicitly into space-separated parts, and only then parse those as integers. For example, like this:

String input = scan.nextLine();
for(String part : input.split(" "))
    sum += Integer.parseInt(part);

Serge Seredenko's answer is also correct, however, but that's another problem.

于 2013-10-06T02:55:52.700 回答
0

除了 while 循环之后的分号(;)之外,您的代码中的所有内容都很好,当然它会导致无限循环。

于 2013-10-06T03:00:09.727 回答
0
int counter = 0 , sum = 0;

Scanner scan = new Scanner(System.in);

System.out.println("enter up to 5 numbers");

while(scan.hasNextInt()){
    counter++;
    sum += scan.nextInt();
    if(counter >=5)
        break;
}
System.out.println(counter);
System.out.println(sum);
scan.close();

首先,您需要删除 ';' 位于while(scan.hasNextInt())之前和之后{;对于;手段,while陈述是完整的。其次,当你使用你的代码时,你需要CTRL + Z结束你的输入。通过添加

if(counter >=5)
    break;

当您输入 5 个数字时,您的输入将结束。

于 2013-10-06T03:04:18.817 回答
0

如果您想读取整行然后稍后进行算术运算,那么您不需要使用 hasNextInt() 方法进行 while 循环。

我建议您阅读 line 然后按空格拆分并遍历字符串数组。查看代码片段。

package com.gaurangjadia.code.java;

import java.util.Scanner;

public class SO19204901 {

    public static void main(String[] args) {
        int counter = 0,
                sum = 0;

        System.out.println("enter up to 5 numbers");

        Scanner scan = new Scanner(System.in);

        String strInput = scan.nextLine();

        String[] arrayNumbers = strInput.split(" ");

        for (int i = 0; i < arrayNumbers.length; i++) {
            int n;

            try {
                n = Integer.parseInt(arrayNumbers[i]);
            }
            catch (NumberFormatException e) {
                n = 0;
            }

            sum = sum + n;
            counter++;
        }

        System.out.println(sum);

    }

}
于 2013-10-06T04:22:59.603 回答
0
  DataInputStream in = new DataInputStream(System.in);
  String[]label = {"quiz #","Total","Average"};
  int counter = 0;
  int theSum = 0;

   System.out.print("Enter up to 5 number : ");
   String[]tempNum = in.readLine().trim().split("\\s+");
   System.out.println();

 while (counter <= tempNum.length)
 {
    if ( counter == tempNum.length)
    {
     System.out.printf("%10s %12s\n",label[1],label[2]);
     counter = 0;
     break;
    } else {
     System.out.printf("%10s",label[0] + (counter+1) );
    }
    counter++;  
 }

 while(counter <= tempNum.length)
 {
    if ( counter == tempNum.length)
    {System.out.printf("%10d %10.2f\n",theSum,(double)(theSum/counter));
    } else 
    {System.out.printf("%10d",Integer.valueOf(tempNum[counter]));
     theSum += Integer.valueOf(tempNum[counter]);
    }
    counter++;
 }
于 2013-10-06T08:57:43.890 回答