Day03_java流程控制 使用者交換Scanner
阿新 • • 發佈:2020-11-17
Scanner物件
-
之前我們學的基本語法中我們並沒有實現程式和人的互動,但是Java給我們提供了這樣一個工具類,我們可以獲取使用者的輸入。java.util.Scanner是Java5的新特徵,我們可以通過Scanner類來獲取使用者的輸入。
-
基本語法:
Scanner s = new Scanner(System.in);
-
通過Scanner類的next()與nextLine()方法獲取輸入的字串,在讀取前我們一般需要使用hasNext()與hasNextLine()判斷是否還有輸入的資料。
使用next方式接收
package com.lemon.scanner; import java.util.Scanner; public class Demo01 { public static void main(String[] args) { //建立一個掃描物件,用於接收鍵盤資料 Scanner scanner = new Scanner(System.in); System.out.println("使用next方式接收:"); //判斷使用者有沒有輸入字串 if(scanner.hasNext()){ //使用next方式接收 String str = scanner.next(); System.out.println("輸入的內容為:"+str); } //IO流就是電腦的輸入輸出 /*IO基本概念:Java對資料的操作是通過流的方式,IO流用來處理裝置之間的 資料傳輸,上傳檔案和下載檔案,Java用於操作流的物件都在IO包中。 */ //凡是屬於IO流的類如果不關閉會一直佔用資源,要養成好習慣用完就關掉 scanner.close(); } }
//執行結果
使用next方式接受:
使用next方式接受:
helloWorld!
輸入的內容為:helloWorld!
Process finished with exit code 0
使用nextLine方式接收
package com.lemon.scanner; import java.util.Scanner; public class Demo02 { public static void main(String[] args) { //只需打出new Scanner(System.in);點選小燈泡選擇introduce... //從鍵盤接收資料 Scanner scanner = new Scanner(System.in); System.out.println("使用nextLine方式接收:"); if(scanner.hasNextLine()){ //使用next方式接收 String str = scanner.nextLine();//程式會等待使用者輸入完畢 System.out.println("輸出內容為:"+str); } //凡是屬於IO流的類如果不關閉會一直佔用資源,要養成好習慣用完就關掉 scanner.close(); } }
使用nextLine方式接收:
hello World
輸出內容為:hello World
Process finished with exit code 0
package com.lemon.scanner; import java.util.Scanner; public class Demo03 { public static void main(String[] args) { //只需打出new Scanner(System.in);點選小燈泡選擇introduce... //從鍵盤接收資料 Scanner scanner = new Scanner(System.in); System.out.println("請輸入資料:"); //使用next方式接收 String str = scanner.nextLine();//程式會等待使用者輸入完畢 System.out.println("輸出內容為:"+str); //凡是屬於IO流的類如果不關閉會一直佔用資源,要養成好習慣用完就關掉 scanner.close(); } }
請輸入資料:
hi 你好!
輸出內容為:hi 你好!
Process finished with exit code 0
-
next()
- 一定要讀取到有效字元後才可以結束輸入。
- 對輸入有效字元之前遇到的空白,next()方法會自動將其去掉。
- 只有輸入有效字元後才將其後面輸入的空白作為分隔符或者結束符。
- next()不能得到帶有空格的字串。
-
nextLine()
- 1、以Enter為結束符,也就是說nextLine()方法返回的是輸入回車之前的所有字元。
- 可以獲得空白。
package com.lemon.scanner; import java.util.Scanner; public class Demo04 { public static void main(String[] args) { 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.close(); } }
scanner進階應用
package com.lemon.scanner; import java.util.Scanner; public class Demo05 { public static void main(String[] args) { //我們可以輸入多個數字,並求其總和的平均數,每輸入一個數字用回車確認,通過非數字來結束輸入並輸出執行結果 Scanner scanner = new Scanner(System.in); //和 double sum = 0; //計算輸入了多少個數字 int m = 0; //通過迴圈判斷是否還有輸入,並在裡面對第一次就行求和統計 System.out.println("請輸入資料:"); while (scanner.hasNextDouble()){ double x = scanner.nextDouble(); m = m + 1; sum = sum + x; System.out.print("你輸入了第"+m+"個數據,當前結果sum為"+sum); } System.out.println(m+"個數的和為"+sum); System.out.println(m+"個數的平均數是"+(sum/m)); scanner.close(); } }