Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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 读取用户输入_Java - Fatal编程技术网

Java 读取用户输入

Java 读取用户输入,java,Java,我试图让用户输入最多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.hasNextI

我试图让用户输入最多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);

你把一个
介于
之间,而
{
之间,因此它会循环。将其删除。

Scanner.hasnetint()
不会执行您似乎认为它会执行的操作。它不会告诉您在已键入的输入中是否有可用的整数(它对“已键入”的内容没有任何概念),而是是否可以将等待的输入读取为整数。如果没有已等待的输入,它将一直阻塞,直到有输入为止,因此您的循环只是永远坐在那里,阻塞更多的输入

您可能想做的是读取整行,然后将其显式拆分为以空格分隔的部分,然后再将这些部分解析为整数。例如:

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

Serge Seredenko的回答也是正确的,但是这是另一个问题。

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

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.hasnetint())
之后和
{
之前的“;”;因为
表示
while
语句已完成。 第二,当你使用你的代码时,你需要
CTRL+Z
来结束你的输入

if(counter >=5)
    break;

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

如果您想读取整行数据,然后稍后执行算术运算,则不需要使用hasnetint()方法进行while循环

我建议你们先读一行,然后按空格拆分,然后在字符串数组上迭代

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

    }

}
package com.gaurangjadia.code.java;
导入java.util.Scanner;
公共类SO19204901{
公共静态void main(字符串[]args){
int计数器=0,
总和=0;
System.out.println(“最多输入5个数字”);
扫描仪扫描=新扫描仪(System.in);
strInput=scan.nextLine();
字符串[]ArrayNumber=strInput.split(“”);
for(int i=0;i
DataInputStream in=新的DataInputStream(System.in);
String[]label={“quick#”,“Total”,“Average”};
int计数器=0;
int theSum=0;
系统输出打印(“最多输入5个数字:”);
字符串[]tempNum=in.readLine().trim().split(\\s+);
System.out.println();

while(counter)除非控制台中的I/n,否则它仍然不会结束。@Flow很抱歉,直到您在控制台中看到什么?
hasnetint()
blocks?您确定吗?那么它的目的是什么?它的目的是检查即将到来的输入是否可以被解析为整数,或者它是否是其他东西,比如文本。
  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++;
 }