[Java] 取得文字輸入 Scanner BufferedReader
1. Scanner
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入字串: ");
System.out.println("輸入字串為: " + scanner.next());
}
}
nextInt():取得使用者輸入的整數
nextFloat():取得使用者輸入的浮點數
nextLine(): 取得使用者輸入的字串(包含空白字元、空白鍵、Tab)
next(): 取得使用者輸入的字串(不含空白字元、空白鍵、Tab)
2. BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader
(new InputStreamReader(System.in));
String str = bufferedReader.readLine();
System.out.println(str);
}
}
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入字串: ");
System.out.println("輸入字串為: " + scanner.next());
}
}
nextInt():取得使用者輸入的整數
nextFloat():取得使用者輸入的浮點數
nextLine(): 取得使用者輸入的字串(包含空白字元、空白鍵、Tab)
next(): 取得使用者輸入的字串(不含空白字元、空白鍵、Tab)
2. BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader
(new InputStreamReader(System.in));
String str = bufferedReader.readLine();
System.out.println(str);
}
}
留言
張貼留言