Scanner對象
實現程序和人的交互
創建掃描器對象,用於接受鍵盤對象
IO流的類用完就關閉
Scanner類為IO流類型的類
scanner.close();
/**
* Scanner sc = new Scanner(System.in);
* System.out.println("請輸入內容");
* String str = sc.next();
* System.out.println(str);
* String str = sc.nextLine();
* System.out.println(str);
* sc.close();
*/
/**
* nextline與next區別
* next以空格作為結束符號
* nextline以回車作為輸出結束符號
*/
/**
* Scanner scanner = new Scanner(System.in);
* int i = 0;
* float f = 0.0F;
* System.out.println("請輸入整數");
* if(scanner.hasNextInt()){
* i = scanner.nextInt();
* System.out.println("整數數據"+i);
* }else{
* System.out.println("輸入的不是整數");
* }
*
* System.out.println("請輸入小數");
* if(scanner.hasNextFloat()){
* f = scanner.nextFloat();
* System.out.println("小數數據"+f);
* }else{
* System.out.println("輸入的不是小數");
* }
*/
/**
* Scanner sc = new Scanner(System.in);
* int num = 0;
* //計算輸入的數字字數
* Double sum = 0.0;
* //求和
* System.out.println("請輸入數據");
* while(sc.hasNextDouble()){
* double x = sc.nextDouble();
* num++;
* sum+=x;
* }
* System.out.println("sum="+sum);
* System.out.println("avg="+sum/num);
* sc.close();
*/
總結:while循環判斷是否還有輸入