Java输入/输出MyProgrammingLab 21212
我试图解决MyProgrammingLab中的一个问题,其中我必须使用名为input的scanner变量和名为total的整型变量来接受input中的所有整数值,并将它们放入total中。程序建议使用while循环;但是,我不知道会出现什么情况,因为扫描仪绑定到(system.in)而不是文件,所以它接受随机用户输入,而不是预定义的字符串。有人能提供建议吗?以下是我尝试使用的当前代码:Java输入/输出MyProgrammingLab 21212,java,Java,我试图解决MyProgrammingLab中的一个问题,其中我必须使用名为input的scanner变量和名为total的整型变量来接受input中的所有整数值,并将它们放入total中。程序建议使用while循环;但是,我不知道会出现什么情况,因为扫描仪绑定到(system.in)而不是文件,所以它接受随机用户输入,而不是预定义的字符串。有人能提供建议吗?以下是我尝试使用的当前代码: int number = 0; while (input.hasNext()) { number
int number = 0;
while (input.hasNext())
{
number = input.nextInt();
total = number;
}
我收到一条消息,字面上只读取(“编译器错误”),而不理解发生了什么。我知道hasnext不会工作,但即使我删除它,我也会收到相同的编译错误消息;此外,我不确定在没有hasNext方法的情况下使用while条件
我尝试更改为input.hasNextLine,因为有两个人认为这可能是一个EOF REACH错误,但我仍然得到了一个编译器错误。可能重复或可能重复的解决方案似乎是在.hasnext方法后添加行,但这并没有解决问题。
/*
I am trying to solve a problem in MyProgrammingLab in which I must use a scanner variable named input,
and an integer variable named total, to accept all the integer values in input, and put them into total.
The program recommends a while loop;
*/
private static Scanner input; //Collect user numbers
private static int total = 0; // to display total at end
private static int number = 0; // Variable to hold user number
private static boolean loopCheck = true; // Condition for running
public static void main(String[] args) {
// Main loop
while(loopCheck) {
input = new Scanner(System.in); // Setting up Scanner variable
// Information for user
System.out.println("Enter 0 to finish and display total");
System.out.println("Enter an Integer now: ");
number = input.nextInt(); // This sets the input as a variable (so you can work with the information)
total += number; // total = (total + number); Continually adds up.
// When user inputs 0, changes boolean to false, stops the loop
if(number == 0) {
loopCheck = false;
}
}
//Displays this when 0 is entered and exits loop
System.out.println("Your total is: " + total); // Displays the total here
System.out.println("Program completed");
}