public class ScannerTest { public static void main(String...args){
Scanner scanner = new Scanner(System.in);
System.out.println("input a number:");
while(true){
int score = 0;
try{
score = scanner.nextInt();
}catch(Exception e){
// e.printStackTrace();
System.out.println("illegal number");
continue;//这个地出现死循环
}
switch(score/10){
case 10:
case 9:
System.out.println("Excellent");
break;
case 8:
System.out.println("Good");
break;
case 7:
case 6:
System.out.println("Pass");
break;
default:
System.out.println("Fail");
break;
}
}
}
}
当用户输入的不是数字时,会死循环,请问为什么?
Scanner scanner = new Scanner(System.in);
System.out.println("input a number:");
while(true){
int score = 0;
try{
score = scanner.nextInt();
}catch(Exception e){
// e.printStackTrace();
System.out.println("illegal number");
continue;//这个地出现死循环
}
switch(score/10){
case 10:
case 9:
System.out.println("Excellent");
break;
case 8:
System.out.println("Good");
break;
case 7:
case 6:
System.out.println("Pass");
break;
default:
System.out.println("Fail");
break;
}
}
}
}
当用户输入的不是数字时,会死循环,请问为什么?
解决方案 »
- JNA时,对Handle 类型的处理
- java中图片处理问题
- 错误提示不明白
- java eclipse 词频统计系统 急求助!
- setVisible()问题,高手请进!
- Jpanel怎么把里面的组件全部除去(**********)
- 进来看一下Array 和 Vector equals() 的方法的问题
- 小弟想用java实现像c++中的缺省值的功能,不知如何实现。希望那位大虾指点一二。多谢,一定给分!
- 这个applet为什么显示不出来(附源码)
- 求多数组的排列组合算法
- 各位大侠们···来帮我解决下这个—序列化的问题,谢谢··急急急!
- 一个Spring jdbc getJdbcTemplate() queryForObject问题
因为你写了continue 写break就不会了
把定义scanner放到循环体里就得了import java.util.*;
public class ScannerTest { public static void main(String...args){
System.out.print("input a number:");
while(true){
Scanner scanner = new Scanner(System.in);
int score = 0;
boolean flag = true;
try{
score = scanner.nextInt();
}catch(Exception e){
System.out.println("illegal number");
continue;
}
switch(score/10){
case 10:
case 9:
System.out.println("Excellent");
break;
case 8:
System.out.println("Good");
break;
case 7:
case 6:
System.out.println("Pass");
break;
default:
System.out.println("Fail");
break;
}
}
}
}
可以用BufferedReader
代替Scanner
以前做POJ的题目
同一个程序,我用Scanner的,一看运行时间,2000多ms
换成BufferedReader,立马变成了300多ms
int i = sc.nextInt();
再看一个例子,以下代码使 long 类型可以通过 myNumbers 文件中的项分配: Scanner sc = new Scanner(new File("myNumbers"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
}扫描器还可以使用不同于空白的分隔符。下面是从一个字符串读取若干项的例子: String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close(); 输出为: 1
2
red
blue 以下代码使用正则表达式同时解析所有的 4 个标记,并可以产生与上例相同的输出结果: String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i));
s.close(); 扫描器所使用的默认空白分隔符通过 Character.isWhitespace 来识别。不管以前是否更改,reset() 方法将把扫描器分隔符的值重置为默认空白分隔符。 扫描操作可能被阻塞,而等待信息的输入。
并没有做光标移动操作(没有跳过这个非数字),
然后跳到了catch块中,
继续下一次循环,
由于上一次的光标没有跳过非数字,
所以继续sc.nextInt();
得到的还是上次输入的非数字,
出现了死循环。
while(true){
Scanner scanner = new Scanner(System.in);
int score = 0;如果像上面的代码,将扫描器在循环内部初始化,那每次sc都会空的,然后监听用户的输入,
就算遇到异常,下次循环时会重新初始化,就不会遇到死循环了!希望有高人解答。。我也乱猜的
import java.util.Scanner;
public class ScannerTest { public static void main(String...args){
Scanner scanner = new Scanner(System.in);
System.out.println("input a number:");
while(true){
int score = 0;
try{
score = scanner.nextInt();
}catch(Exception e){
// e.printStackTrace();
System.out.println("illegal number");
String temp = scanner.next();//早上理解错了,在这里加上这行就好了,如果没有这一行
//,当你输入的不是数字的时候,就会执行catch里面的代码,continue到循环开始处继续,下次读取的时候
//还是前面输入的非数字字符,所以继续抛出异常,这样就成两个一个死循环,所以可以在发生异常后处理掉
//输入的非数字字符
continue;//这个地出现死循环
}
switch(score/10){
case 10:
case 9:
System.out.println("Excellent");
break;
case 8:
System.out.println("Good");
break;
case 7:
case 6:
System.out.println("Pass");
break;
default:
System.out.println("Fail");
break;
}
}
}
}
程序中做了注释!
把flag作为main方法的全局变量把他放在while(flag)中再在每个case语句中break之前加入flag=false;
这样就可以跳出循环了具体代码如下public class ScannerTest { public static void main(String...args){
boolean flag = true;
System.out.print("input a number:");
while(flag){
Scanner scanner = new Scanner(System.in);
int score = 0;
try{
score = scanner.nextInt();
}catch(Exception e){
System.out.println("illegal number");
continue;
}
switch(score/10){
case 10:
case 9:
System.out.println("Excellent");
flag=false;
break;
case 8:
System.out.println("Good");
flag=false;
break;
case 7:
case 6:
System.out.println("Pass");
flag=false;
break;
default:
System.out.println("Fail");
flag=false;
break;
}
}
}
}
Scanner scanner = new Scanner(System.in);
System.out.println("input a number:");
while(true){
String scoreStr;
int score;
try{
scoreStr = scanner.next();
score=Integer.parseInt(scoreStr);
}catch(Exception e){
// e.printStackTrace();
System.out.println("illegal number");
continue;
}
switch(score/10){
case 10:
case 9:
System.out.println("Excellent");
break;
case 8:
System.out.println("Good");
break;
case 7:
case 6:
System.out.println("Pass");
break;
default:
System.out.println("Fail");
break;
}
}
}
}1 这样可以运行.我觉得挺好的
2 程序建议有个提示请输入[0-100]之间的一个数
3 大于100的数应该提示illegal而不是走到switch的default "Fail"
多謝,我也試過通過parseInt不會死徨環,你的提議也很好,謝謝。謝謝大家,結帖了