Java鍵盤錄入的三種方式

Java鍵盤錄入三種方法

方法一:Scanner類中的方法

優點一: 可以獲取鍵盤輸入的字符串
優點二: 有現成的獲取int,float等類型數據,非常強大,也非常方便

方法二:System.in和System.out方法 (使用read)

缺點一: 該方法能獲取從鍵盤輸入的字符,但只能針對一個字符的獲取
缺點二: 獲取的只是char類型的。如果想獲得int,float等類型的輸入,比較麻煩。

方法三:InputStreamReader和BufferedReader方法 (使用readline)

優點: 可以獲取鍵盤輸入的字符串
缺點: 如何要獲取的是int,float等類型的仍然需要轉換

注意,方法二、方法三都藉助於read()方法,其爲阻塞時方法,既等待輸入後纔會繼續運行。

以下爲程序實例。

/**
 * 鍵盤錄入
 * @author        段
 * @date          2019.12.31
 
 */
import java.io.*;
import java.util.*;
public class KeyRu{
    public static void main(String[] args){//main函數
        SystemCharTest();//調用System.in方法
        ReadStringTest();//調用readline方法,使用BuffererReader緩衝區與InputStreamReader流
        ScannerAllTest();//調用Scanner方法
    }
public static void ScannerAllTest(){
    Scanner read=new Scanner(System.in);
    System.out.println("please enter name");
    String name =read.nextLine();//read String  keyboard entry
    System.out.println("please enter age");
    int age=read.nextInt();//讀取整數輸入
    System.out.println("Please entry score");
    double score=read.nextDouble();//讀取double輸入
    System.out.println("The information you enter is as follows:");
    System.out.print(name+"\t"+age+"\t"+score);
}
public static void SystemCharTest(){
    try{    //該方法中有個IOExcepiton需要捕獲 
        System.out.println("Please enter a char");
        char c =(char)System.in.read();
        System.out.println ("The char that you entry is \t"+ c);
    }
    catch(IOException ex){
        
        ex.printStackTrace();
    }
}
public static void ReadStringTest(){
    System.out.println("please enter string");
    /*InputStreamReader is = new InputStreamReader(System.in); //new構造InputStreamReader對象 
    BufferedReader br = new BufferedReader(is); //拿構造的方法傳到BufferedReader中 */
//    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//簡化
    try(BufferedReader br=new BufferedReader(new InputStreamReader(System.in));)//try塊退出時,會自動調用res.close()方法,關閉資源。
    { //該方法中有個IOExcepiton需要捕獲 
      String name = br.readLine(); 
      System.out.println("ReadTest Output:" + name); 
    } 
    catch(IOException ex){ 
      ex.printStackTrace(); 
    }
}
}

注意:每個方法應單獨演示。

路阻且長之Java學習:

API中的重要類(一):
https://blog.csdn.net/Veer_c/article/details/103803248
API中的重要類(二):
https://blog.csdn.net/Veer_c/article/details/103807515
API中的重要類(三):
https://blog.csdn.net/Veer_c/article/details/103808054

Java中的IO流(一):
https://blog.csdn.net/Veer_c/article/details/103833045
Java中的IO流(二):
https://blog.csdn.net/Veer_c/article/details/103833423
Java中的IO流(三):
https://blog.csdn.net/Veer_c/article/details/103833811

Java多線程(一):
https://blog.csdn.net/Veer_c/article/details/103842078
Java多線程(二):
https://blog.csdn.net/Veer_c/article/details/103842263
Java多線程(三):
https://blog.csdn.net/Veer_c/article/details/103842317
Java多線程(四):
https://blog.csdn.net/Veer_c/article/details/103842602

網絡編程上(UDP):
https://blog.csdn.net/Veer_c/article/details/103843591
網絡編程下(TCP):
https://blog.csdn.net/Veer_c/article/details/103843825

MySQL數據庫(一):
https://blog.csdn.net/Veer_c/article/details/103844059
MySQL數據庫(二):
https://blog.csdn.net/Veer_c/article/details/103844537
MySQL數據庫(三):
https://blog.csdn.net/Veer_c/article/details/103844739

JDBC技術(一):
https://blog.csdn.net/Veer_c/article/details/103845176
JDBC技術(二):
https://blog.csdn.net/Veer_c/article/details/103879890
JDBC技術(三):
https://blog.csdn.net/Veer_c/article/details/103880021
JDBC技術(四):
https://blog.csdn.net/Veer_c/article/details/103882264

HTML的基礎框架(一):
https://blog.csdn.net/Veer_c/article/details/103882385
HTML的基礎框架(二):
https://blog.csdn.net/Veer_c/article/details/103882684

CSS入門(一)
https://blog.csdn.net/Veer_c/article/details/103882856

CSS入門(二):
https://blog.csdn.net/Veer_c/article/details/103883102

JavaScript實用案例與常見問題(一):
https://blog.csdn.net/Veer_c/article/details/103894959
JavaScript實用案例及常見問題(二):
https://blog.csdn.net/Veer_c/article/details/103895166

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章