Java中接收键盘输入的三种方法
作者:junjie 时间:2023-11-13 16:11:29
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) { //主方法
CharTest(); //调用System.in方法
ReadTest(); //调用ReadTest方法
ScannerTest();//调用ScannerTest方法
}
/**
* System.in和System.out方法
* 缺点一: 该方法能获取从键盘输入的字符,但只能针对一个字符的获取
* 缺点二: 获取的只是char类型的。如果想获得int,float等类型的输入,比较麻烦。
*/
public static void CharTest(){
try{
System.out.print("Enter a Char:");
char i = (char)System.in.read();
System.out.println("Yout Enter Char is:" + i);
}
catch(IOException e){
e.printStackTrace();
}
}
/**
* InputStreamReader和BufferedReader方法
* 优点: 可以获取键盘输入的字符串
* 缺点: 如何要获取的是int,float等类型的仍然需要转换
*/
public static void ReadTest(){
System.out.println("ReadTest, Please Enter Data:");
InputStreamReader is = new InputStreamReader(System.in); //new构造InputStreamReader对象
BufferedReader br = new BufferedReader(is); //拿构造的方法传到BufferedReader中
try{ //该方法中有个IOExcepiton需要捕获
String name = br.readLine();
System.out.println("ReadTest Output:" + name);
}
catch(IOException e){
e.printStackTrace();
}
}
/**
* Scanner类中的方法
* 优点一: 可以获取键盘输入的字符串
* 优点二: 有现成的获取int,float等类型数据,非常强大,也非常方便;
*/
public static void ScannerTest(){
Scanner sc = new Scanner(System.in);
System.out.println("ScannerTest, Please Enter Name:");
String name = sc.nextLine(); //读取字符串型输入
System.out.println("ScannerTest, Please Enter Age:");
int age = sc.nextInt(); //读取整型输入
System.out.println("ScannerTest, Please Enter Salary:");
float salary = sc.nextFloat(); //读取float型输入
System.out.println("Your Information is as below:");
System.out.println("Name:" + name +"\n" + "Age:"+age + "\n"+"Salary:"+salary);
}
}
总结:
要获取键盘中的输入:
python提供了,python2中有raw_input(),python3中有input()。
c提供了scanf()函数
c++提供cin()函数来获取键盘输入
java中没有现成的函数来获取键盘输入,但仍然可以用上述方法来实现,其中方法三应该是最简单,最方便的。
标签:Java,接收,键盘,输入
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ShardingSphere数据分片算法及测试实战
2023-11-28 02:23:03
![](https://img.aspxhome.com/file/2023/8/59768_0s.png)
C#中使用UDP通信的示例
2022-11-19 21:09:07
![](https://img.aspxhome.com/file/2023/0/106520_0s.png)
IDEA部署JavaWeb项目到Tomcat服务器的方法
2023-11-02 23:21:22
java编程求二叉树最大路径问题代码分析
2023-03-16 20:44:16
Android实现拍照或者选取本地图片
2022-06-16 03:58:31
![](https://img.aspxhome.com/file/2023/6/120066_0s.jpg)
全面了解Java中的内部类和匿名类
2023-01-05 09:14:23
详解Java豆瓣电影爬虫——小爬虫成长记(附源码)
2023-10-29 17:45:17
![](https://img.aspxhome.com/file/2023/7/84287_0s.png)
Android RecyclerView添加搜索过滤器的示例代码
2022-03-08 21:44:49
![](https://img.aspxhome.com/file/2023/8/108278_0s.gif)
jpa EntityManager 复杂查询实例
2023-08-31 01:03:01
Java获取当前操作系统的信息实例代码
2023-03-07 20:00:08
Spring Cloud升级最新Finchley版本的所有坑
2021-09-02 07:21:51
![](https://img.aspxhome.com/file/2023/3/88593_0s.png)
IntelliJ IDEA最佳配置(推荐)
2023-11-17 01:55:44
![](https://img.aspxhome.com/file/2023/1/128831_0s.png)
Android中的Launch Mode详情
2022-10-06 02:38:09
![](https://img.aspxhome.com/file/2023/3/125393_0s.webp)
golang实现java uuid的序列化方法
2022-12-30 11:30:57
ListView的View回收引起的checkbox状态改变监听等问题解决方案
2023-07-26 17:33:45
OpenCV实现直线检测并消除
2023-07-12 20:44:36
![](https://img.aspxhome.com/file/2023/3/92493_0s.jpg)
windows下jar包开机自动重启的步骤
2022-10-17 22:21:41
java并发包JUC诞生及详细内容
2022-04-06 02:57:00
SpringBoot Knife4j在线API文档框架基本使用
2022-03-10 21:27:48
C#入门教程之集合ArrayList用法详解
2022-04-30 06:32:13
![](https://img.aspxhome.com/file/2023/8/119568_0s.png)