1. 程式人生 > 其它 >【Java學習筆記】【流程控制】08.使用者互動Scanner

【Java學習筆記】【流程控制】08.使用者互動Scanner

使用者互動Scanner

1.Scanner物件

之前我們學的基本語法中我們並沒有實現程式和人的互動,但是Java給我們提供了這樣一個工具類,我們可以獲取使用者的輸入。

java.util.Scanner是Java5的新特徵,我們可以通過Scanner類來獲取使用者的輸入。

下面是建立Scanner物件的基本語法:

Scanner s = new Scanner(System.in);

2.next()與nextLine()

①用next()方法接收資料:

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner ming = new Scanner(System.in);     //1.建立一個掃描器物件ming,用於接收鍵盤資料
        
        System.out.println("使用next方法接收:");     //2.提示使用者輸入
        if(ming.hasNext()){                         //3.判斷使用者有沒有輸入字元
            String str = ming.next();               //4.如果輸入,使用next()方法接收,將輸入的內容放入str中
            System.out.println("輸出的內容為:"+str); //5.輸出str中的內容
        }
    
        ming.close();       //凡是屬於I/O流的類如果不關閉會一直佔用資源.要養成好習慣用完就關掉。
    }
}

測試用例:
使用next方法接收:
Hello World
輸出的內容為:Hello

②用nextLine()方法接收資料:

public class Demo01 {
    public static void main(String[] args) {
        Scanner ming = new Scanner(System.in);      //1.建立一個掃描器物件ming,用於接收鍵盤資料
        
        System.out.println("使用next方法接收:");     //2.提示使用者輸入
        if(ming.hasNextLine()){                     //3.判斷使用者有沒有輸入字元
            String str = ming.nextLine();           //4.如果輸入,使用next()方法接收,將輸入的內容放入str中
            System.out.println("輸出的內容為:"+str); //5.輸出str中的內容
        }
       
        ming.close();        //凡是屬於I/O流的類如果不關閉會一直佔用資源.要養成好習慣用完就關掉。
    }
}

測試用例:
使用next方法接收:
Hello World
輸出的內容為:Hello World    

3.next()與nextLine()區別

next():

  • 一定要讀取到有效字元後才可以結束輸入;
  • 對輸入有效字元之前遇到的空白,next()方法會自動將其去掉;
  • 只有輸入有效字元後才將其後面輸入的空格作為分隔符或者結束符;
  • next()不能得到帶有空格的字串。

nextLine():

  • 以Enter為結束符,也就是說nextLine()方法返回的是輸入回車之前的所有字元;
  • 可以獲得空格。

4.Scanner進階使用

當用Scanner輸入int或float等型別的資料時,Scanner也有支援。

import java.util.Scanner;

public class Demo01 {
    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("輸入的不是小數");
        }
    }
}
測試用例1:
請輸入整數:
10
整數資料:10
請輸入小數:
1.1
小數資料:1.1

測試用例2:
請輸入整數:
10.1
輸入的不是整數
請輸入小數:
小數資料:10.1    

5.例題:

輸入多個數字,並求其總和與平均數,每輸入一個數字用回車確認,通過輸入非數字來結束輸入並輸出執行結果。

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入資料:");
        double sum = 0;
        int m = 0;
        //判斷迴圈判斷是否還有輸入,並在裡面每一次進行求和和統計
        while(scanner.hasNextDouble()){
            double x = scanner.nextDouble();
            ++m;
            sum=sum+x;
            System.out.println("你輸入了第" + m +"個數據,當前結果是sum=" + sum);
        }
        System.out.println(m + "個數的總和為" + sum);
        System.out.println(m + "個數的平均數為" + (sum/m));

        scanner.close();
    }
}

測試用例:
請輸入資料:
10
你輸入了第1個數據,當前結果是sum=10.0
20
你輸入了第2個數據,當前結果是sum=30.0
30
你輸入了第3個數據,當前結果是sum=60.0
40
你輸入了第4個數據,當前結果是sum=100.0
a
4個數的總和為100.0
4個數的平均數為25.0