问题描述
我在理解代码执行顺序方面遇到了很大的麻烦。 我的程序必须接受10个整数输入并进行计算。因此,我使用forloop接受10个输入。 在得到很少的错误并进行处理之后,我终于编写了没有错误的代码。但是我完全不知道它是如何工作的。 该程序很简单。请看看
public static void main(String[] args) {
int count = 0; //initializing the total sum as 0
Scanner scanner = new Scanner(system.in);
for (int i=1; i<=10; i++){
System.out.println("Enter "+i+"th value :");
boolean hasNextInt = scanner.hasNextInt(); //to check if the input is integer
if(hasNextInt) {
int value = scanner.nextInt(); //taking input here. here is my doubt
count += value;
}
else {
System.out.println(" invalid value");
i--;
}
scanner.nextLine();
}
System.out.println("The sum of the values are "+count);
scanner.close();
}
现在,输入在给定程序的第11行中输入。但是整数条件正在第10行检查。到第10行,它甚至根本没有输入。如何检查输入? 这个顺序对我来说没有意义。 谢谢您的解释。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)