码迷,mamicode.com
首页 > 其他好文 > 详细

2016/06/02学习记录

时间:2016-06-03 08:43:08      阅读:150      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!