【发布时间】:2015-06-25 17:05:47
【问题描述】:
public class Main{
public static void main(String args[]){
int i = nextInt();
}
public int nextInt(){
int i=0;
boolean done=false;
Scanner scanner = new Scanner(System.in);
while (!scanner.hasNextInt()){
scanner.nextLine();
Printer.println(Printer.PLEASE_NUMBER);
}
i=scanner.nextInt();
scanner.close();
return i;
}
}
上面的代码是我试图强制用户输入一个 int 值的方式,但我得到了 nosuchelement 异常,因为scanner.nextLine() 读取的是NULL。 在 C++ 中,软件等待用户输入一些东西。有什么办法可以强制程序停止,等待用户输入内容然后进行检查?
编辑: 所以无论如何我都会遇到问题,如果我在 Main 类之外使用扫描仪,它会给出错误...
【问题讨论】:
-
请发布完整程序!
标签: java loops input nosuchelementexception