yNdots
需要及时鞭策的测试汪

@version 4.3.2015

@author yNdots 

Java中用到的最普通的从键盘读入数据的代码为Scanner sc=new Scanner(System.in); 这样实例化一个Scanner类之后。这里要提到的是System类作为一个最终类,其具有3个字段,2个为PrintStream类型,即err和out,另外一个就是InputStream类型的in。

但是我遇到一个问题,我从键盘输入一个字符串" ad 132 ",代码如下:

 

1           Scanner sc=new Scanner(System.in);
2         while(sc.hasNext()){
3 //            System.out.print(sc.nextLine()+" ");/*正确类型*/
4         System.out.print(sc.nextInt()+" ");/*InputMismatchException*/
5 //            System.out.print(sc.next()+" ");/*正确类型*/
6         }
7             sc.close();

 

posted on 2015-03-04 12:18  一条特立独行的狗  阅读(225)  评论(0编辑  收藏  举报