2016/06/02学习记录

1一个异常抛出的小例子

 1 package Demo;
 2 
 3 import java.util.Scanner;
 4 
 5 public class RedTwo {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner in = new Scanner(System.in);
10         System.out.println("Please enter an integer");
11         boolean stop = true;
12         do {
13             try {
14                 Integer c = in.nextInt();
15                 System.out.println("The integer is " + c);
16                 stop = false;
17             } catch (RuntimeException ex) {
18                 System.out.println("The number you enter is not integer");
19                 // System.out.println(in.next());\
20                 in.nextLine();
21             }
22 
23         } while (stop);
24 
25     }
26 
27 }

其中input.nextline()非常关键,如果没有这行,程序进入死循环,因为抛出异常后,输入流标记符还在上次输入的位置,这行代码使输入流标记符移动到下一次输入的位置。

 

posted @ 2016-06-03 08:20  来搞笑的  阅读(135)  评论(0编辑  收藏  举报