1. 程式人生 > 其它 >JAVA今日2021.8.2

JAVA今日2021.8.2

JAVA scanner

Scannner01

package JAVASE.scanner;

import java.util.Scanner;

public class scanner01 {
    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 );
        }
        //會佔用資源,用完就關掉
        scanner.close();

    }
}

Scanner02

package JAVASE.scanner;

import java.util.Scanner;

public class scanner02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextline方式接收:");
        if (scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("輸出的內容為:"+str );

        }
        scanner.close();
    }
}

Scanner03

package JAVASE.scanner;

import java.util.Scanner;

public class scanner03 {
    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();
    }
}

2021.8.2