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

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值