java键盘IO

 1 public class IO {
 2     public static void main(String[] args) throws Throwable {
 3         ScannerTest();
 4 //        testScanner1();
 5 //        testScanner2();
 6         
 7 //        testInRead();
 8         
 9 //        testText();        
10     }
11 
12     /**  
13      * Scanner类中的方法  
14      * 优点一: 可以获取键盘输入的字符串  
15      * 优点二: 有现成的获取int,float等类型数据,非常强大,也非常方便;  
16      * */
17     public static void ScannerTest(){
18         Scanner sc = new Scanner(System.in);   
19         System.out.println("ScannerTest, Please Enter Name:");   
20         String name = sc.nextLine();  //读取字符串型输入   
21         System.out.println("ScannerTest, Please Enter Age:");   
22         int age = sc.nextInt();    //读取整型输入   
23         System.out.println("ScannerTest, Please Enter Salary:");   
24         float salary = sc.nextFloat(); //读取float型输入   
25         System.out.println("Your Information is as below:");   
26         System.out.println("Name:" + name +"
" + "Age:"+age + "
"+"Salary:"+salary);
27     } 
28     
29     
30     /**
31      * 注意:看下边两个实例验证我们读入数据的时机
32      * 有两种方式读入数据:用回车方式分隔数据和用空格分隔数据
33      * 回车分隔数据时:读入数据的时机是在每次执行scanner.nextInt()时
34      * 空格分隔数据时:读入数据的时机是在第一次执行scanner.nextInt()时
35      * 然而,上述规则对读入字符串类型时是无效的,因为空格也算是字符串 
36      * 另外,本类型不支持读取字符
37      */
38     private static void testScanner1() {
39         Scanner scanner = new Scanner(System.in);
40         System.out.println("--1--");
41         int l=scanner.nextInt();
42         int[] num = new int[l];
43         System.out.println("--2--");
44         for(int i = 0; i<l && scanner.hasNext(); i++){
45             System.out.println("--3--");
46             num[i] = scanner.nextInt();
47         }
48         System.out.println("数组输出:" + Arrays.toString(num));
49     }
50     
51     private static void testScanner2(){
52         Scanner s = new Scanner(System.in);
53         System.out.println("--1--");
54         while (s.hasNext()) {
55             System.out.println("--2--");
56             int a = s.nextInt();
57             System.out.println("--3--");
58             int b = s.nextInt();
59             System.out.println("--4--");
60             System.out.println("加和:" + (a + b));
61         }
62     }
63     
64     /**
65      * System.in方法:
66      * 这种方法每次只能读进一个字符,即使接收类型为int型,也不能读两位以上的int类型
67      * 所以这种方法最合适读取字符类型,如果要读取int/float等类型,会很麻烦。局限性很大
68      * */
69     public static void testInRead() throws IOException{        
70         char b = (char)System.in.read();
71         System.out.println("char类型数b:" + b);
72     }
73     
74     /**
75      * InputStreamReader和BufferedReader方法  
76      * 通过输入输出流来读取键盘输入
77      * 此方法读取字符串功能强大
78      * 但是如果要编程char/int/float还需要进行强制类型转换
79      * */
80     public static void testText() throws Throwable{
81         InputStreamReader in = new InputStreamReader(System.in);
82         BufferedReader br = new BufferedReader(in);
83         String str = br.readLine();
84         System.out.println("str:" + str);
85     }
86 }
原文地址:https://www.cnblogs.com/K-artorias/p/7472319.html