Android(java)学習ノート97:Scanner類使用

7268 ワード

 1 package cn.itcast_01;

 2 

 3 /*

 4  * Scanner:          。

 5  * 

 6  *      :

 7  *         A:  

 8  *         B:    

 9  *         C:    

10  * 

11  * System          :

12  *         public static final InputStream in;       ,       。

13  * 

14  *         InputStream is = System.in;

15  * 

16  * class Demo {

17  *         public static final int x = 10;

18  *         public static final Student s = new Student();

19  * }

20  * int y = Demo.x;

21  * Student s = Demo.s;

22  * 

23  * 

24  *     :

25  *         Scanner(InputStream source)

26  */

27 import java.util.Scanner;

28 

29 public class ScannerDemo {

30     public static void main(String[] args) {

31         //     

32         Scanner sc = new Scanner(System.in);

33 

34         int x = sc.nextInt();

35         

36         System.out.println("x:" + x);

37     }

38 }

 
 1 package cn.itcast_02;

 2 

 3 import java.util.Scanner;

 4 

 5 /*

 6  *     :

 7  *         public boolean hasNextXxx():            

 8  *         public Xxx nextXxx():     

 9  * 

10  *   : int       

11  *         public boolean hasNextInt()

12  *         public int nextInt()

13  * 

14  *   :

15  *         InputMismatchException:           

16  */

17 public class ScannerDemo {

18     public static void main(String[] args) {

19         //     

20         Scanner sc = new Scanner(System.in);

21 

22         //     

23         if (sc.hasNextInt()) {

24             int x = sc.nextInt();

25             System.out.println("x:" + x);

26         } else {

27             System.out.println("        ");

28         }

29     }

30 }