標籤:
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 }
Android(java)學習筆記97:Scanner類使用