1. 程式人生 > 其它 >Java之Scanner物件

Java之Scanner物件

Scanner物件

  • 為實現程式和人的互動,Java給我們提供了一個工具類,可以獲取使用者的輸入,java.util.Scanner是Java5的新特徵,可以通過Scanner類來獲取使用者的輸入。

  • 基本語法:

    Scanner s = new Scanner(System.in);
    
  • 通過Scanner類的next()和nextLine()方法獲取輸入的字串,在讀取前我們需要使用hasNext()和hasNextLine()判斷是否還有輸入的資料。

next()方法:

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

public class Demo01 {

    public static void main(String[] args) {

        //建立一個掃描器物件,從鍵盤接收資料
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入資料:");

        //判斷使用者有沒有輸入字串
        if (scanner.hasNext()){
            //使用next()方式接收
            String str = scanner.next();//程式等待使用者輸入完畢
            System.out.println("輸出的內容為:" +str);
        }
       //屬於IO流的類如果不關閉會一直佔用資源,要養成用完就關閉的好習慣
        scanner.close();
    }
//方法二
import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("請輸入資料:");
      
        String str = scanner.nextLine();
        
        System.out.println("輸入的內容為:"+str);
        
        scanner.close();
    }
}

nextLine()方法:

  1. 以Enter為結束符,也就是說 nextLine()方法返回的是輸入回車之前的所有字元。
  2. 可以獲得空白。
import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {

        //建立一個掃描器,從鍵盤接收資料
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入資料:");

        // hasNextLine():判斷是否還有輸入
        if (scanner.hasNextLine()){
        
            //使用nextLine()方式接收
            String str = scanner.nextLine();//程式等待使用者輸入完畢
            System.out.println("輸入的內容為:"+str);
        }
        scanner.close();
    }
}

Scanner的進階使用

import java.util.Scanner;

public class Demo03 {
    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("================================");
        System.out.println("請輸入小數");
            //如果...那麼
            if (scanner.hasNextFloat()){
                f = scanner.nextFloat();
                System.out.println("輸出的小數內容為:"+f);
            }else {
                System.out.println("輸出的內容不是小數:");

        }
        scanner.close();
    }
}
import java.util.Scanner;

public class Demo04 {
    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; //m++
            sum = sum + x;
            System.out.println("你輸入了第"+m+"個數據,當前總和為:"+sum);
        }
        
        System.out.println("當前輸入的個數為:"+ m);
        System.out.println("當前輸出的總和為:"+sum);
        System.out.println("當前輸出的平均值為:"+(sum /m));
        
        scanner.close();
    }
}