Java nextInt()在while循环中存在问题
基本上我需要这个while循环执行4次 它将请求用户输入,然后在嵌套的if语句中使用该输入。(重复4次) 第一个提示将出现,我可以输入 但在那之后,它只会给我一个错误 并一直将错误指向“int userInput=scan.nextInt();” 我不知道它出了什么问题。有人能指引我吗?谢谢Java nextInt()在while循环中存在问题,java,while-loop,java.util.scanner,next,Java,While Loop,Java.util.scanner,Next,基本上我需要这个while循环执行4次 它将请求用户输入,然后在嵌套的if语句中使用该输入。(重复4次) 第一个提示将出现,我可以输入 但在那之后,它只会给我一个错误 并一直将错误指向“int userInput=scan.nextInt();” 我不知道它出了什么问题。有人能指引我吗?谢谢 int count = 0; while (count < 4 ) { System.out.println(prompt1); int userI
int count = 0;
while (count < 4 ) {
System.out.println(prompt1);
int userInput = scan.nextInt();
(some nested if statements here to do my task, they all have
count++ at the end)
}
int count=0;
而(计数<4){
系统输出打印项次(prompt1);
int userInput=scan.nextInt();
(这里的一些嵌套if语句用于执行我的任务,它们都有
计数+(最后)
}
在索取nextInt之前,请务必检查nextInt是否可用:
if(scan.hasNextInt())
{
int userInput = scan.nextInt();
// do something
}
您需要定义扫描
Scanner scan=新的扫描仪(System.in)代码>您遇到了什么错误?在哪里定义和初始化扫描?在while循环外部初始化它我们可以看到该代码吗?同时发布您得到的整个错误/异常。