Ade*_*e A 3 java int loops while-loop
我该怎么说呢:
while(input is not an int){
do this
}
Run Code Online (Sandbox Code Playgroud)
我试过这段代码,但我知道这是错的:
int identificationnumber;
Scanner sc3 = new Scanner(System.in);
identificationnumber = sc3.nextInt();
while( identificationnumber != int){ // this line is wrong
Scanner sc4 = new Scanner(System.in);
identificationnumber = sc4.nextInt();
}
Run Code Online (Sandbox Code Playgroud)
请任何建议.谢谢.
Javadocs是你的朋友:http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html
nextInt()如果下一个标记不是,则抛出异常int.你可能正在寻找hasNextInt()
另外,为什么Scanner每次循环都会创建一个新的?(或者根本就是 - 你已经有一个在循环之前)
尝试:
while (! scanner.hasNextInt()) { // while the next token is not an int...
scanner.next(); // just skip it
}
int i = scanner.nextInt(); // then read the int
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12633 次 |
| 最近记录: |