首页 > 其他 > 详细

2016/06/02学习记录

时间:2016-06-03 08:40:38      阅读:144      评论:0      收藏:0      [点我收藏+]

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()非常关键,如果没有这行,程序进入死循环,因为抛出异常后,输入流标记符还在上次输入的位置,这行代码使输入流标记符移动到下一次输入的位置。

 

2016/06/02学习记录

原文:http://www.cnblogs.com/laigaoxiaode/p/5555033.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!