Java Scanner 類

來源:互聯網
上載者:User

標籤:system.in   next   com   ble   nerd   .com   使用   enter   imp   

java.util.Scanner 是 Java5 的新特徵,我們可以通過 Scanner 類來擷取使用者的輸入。
下面是建立 Scanner 對象的基本文法:
Scanner s = new Scanner(System.in);
接下來我們示範一個最簡單的資料輸入,並通過 Scanner 類的 next() 與 nextLine() 方法擷取輸入的字串,在讀取前我們一般需要 使用 hasNext 與 hasNextLine 判斷是否還有輸入的資料:
使用 next 方法:
ScannerDemo.java 檔案代碼:
import java.util.Scanner;

public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 從鍵盤接收資料

    // next方式接收字串    System.out.println("next方式接收:");    // 判斷是否還有輸入    if (scan.hasNext()) {        String str1 = scan.next();        System.out.println("輸入的資料為:" + str1);    }    scan.close();}

}
執行以上程式輸出結果為:
$ javac ScannerDemo.java
$ java ScannerDemo
next方式接收:
runoob com
輸入的資料為:runoob
可以看到 com 字串並未輸出,接下來我們看 nextLine。
使用 nextLine 方法:
ScannerDemo.java 檔案代碼:
import java.util.Scanner;

public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 從鍵盤接收資料

    // nextLine方式接收字串    System.out.println("nextLine方式接收:");    // 判斷是否還有輸入    if (scan.hasNextLine()) {        String str2 = scan.nextLine();        System.out.println("輸入的資料為:" + str2);    }    scan.close();}

}
執行以上程式輸出結果為:
$ javac ScannerDemo.java
$ java ScannerDemo
nextLine方式接收:
runoob com
輸入的資料為:runoob com
可以看到 com 字串輸出。
next() 與 nextLine() 區別
next():
1、一定要讀取到有效字元後才可以結束輸入。
2、對輸入有效字元之前遇到的空白,next() 方法會自動將其去掉。
3、只有輸入有效字元後才將其後面輸入的空白作為分隔字元或者結束符。
next() 不能得到帶有空格的字串。
nextLine():
1、以Enter為結束符,也就是說 nextLine()方法返回的是輸入斷行符號之前的所有字元。
2、可以獲得空白。
如果要輸入 int 或 float 類型的資料,在 Scanner 類中也有支援,但是在輸入之前最好先使用 hasNextXxx() 方法進行驗證,再使用 nextXxx() 來讀取:
ScannerDemo.java 檔案代碼:
import java.util.Scanner;

public class ScannerDemo {
public bjrongjinhuiyin.com static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 從鍵盤接收資料
int i = 0;
float f = 0.0f;
System.out.print("輸入整數:");
if (scan.hasNextInt()) {
// 判斷輸入的是否是整數
i = scan.nextInt();
// 接收整數
System.out.println("整數資料:" + i);
} else {
// 輸入錯誤的資訊
System.out.println("輸入的不是整數!");
}
System.out.print("輸入小數:");
if (scan.hasNextFloat()) {
// 判斷輸入的是否是小數
f = scan.nextFloat();
// 接收小數
System.out.println("小數資料:" + f);
} else {
// 輸入錯誤的資訊
System.out.println("輸入的不是小數!");
}
scan.close();
}
}
執行以上程式輸出結果為:
$ javac ScannerDemo.java
$ java ScannerDemo
輸入整數:12
整數資料:12
輸入小數:1.2
小數資料:1.2
以下執行個體融金匯銀我們可以輸入多個數字,並求其總和與平均數,每輸入一個數字用斷行符號確認,通過輸入非數字來結束輸入並輸出執行結果:
ScannerDemo.java 檔案代碼:
import java.util.Scanner;

class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

    double sum = 0;    int m = 0;    while (scan.hasNextDouble()) {        double x = scan.nextDouble();        m = m + 1;        sum = sum + x;    }    System.out.println(m + "個數的和為" + sum);    System.out.println(m + "個數的平均值是" + (sum / m));    scan.close();}

}
執行以上程式輸出結果為:
$ javac ScannerDemo.java
$ java ScannerDemo
12
23
15
21.4
end
4個數的和為71.4
4個數的平均值是17.85

Java Scanner 類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.