Linux教程網
Java接收鍵盤輸入的兩種方法
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
-
-
- public class EnterTest {
- public static void main(String[] args) {
- readTest();
- scannerTest();
- }
- public static void readTest(){
- System.out.println("readTest,請輸入數據...");
- InputStreamReader is = new InputStreamReader(System.in);
- BufferedReader br = new BufferedReader(is);
- try {
- String name = br.readLine();
- System.out.println("readTest,輸出:"+name);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void scannerTest(){
- System.out.println("scannerTest,請輸入數據...");
- String name=new Scanner(System.in).nextLine();
- System.out.println("scannerTest,輸出:"+name);
- }
- }
Copyright ©
Linux教程網 All Rights Reserved