答案是使用Scanner类获取键盘输入最常用。首先导入java.util.Scanner,创建Scanner对象读取System.in,用nextLine()读字符串、nextInt()读整数,注意nextInt()后需调用nextLine()消耗换行符,避免nextLine()跳过输入,最后调用close()关闭资源。Scanner封装了字节流到字符的转换,简化数据解析,相比直接使用System.in.read()更高效安全。也可用BufferedReader配合InputStreamReader读取,性能更高但需手动处理类型转换和异常。常见陷阱包括nextInt()遗留换行符导致nextLine()失效,以及用户输入类型不匹配引发InputMismatchException,可通过hasNextInt()预判或try-catch捕获异常处理。务必关闭资源,推荐使用try-with-resources自动管理。清晰提示、输入验证和友好反馈是最佳实践。

在Java中,要从键盘获取用户输入,最常用且推荐的方式是使用
java.util.Scanner
System.in
要使用
Scanner
你首先需要导入
java.util.Scanner
Scanner
System.in
System.in
import java.util.Scanner; // 别忘了导入这个包
public class KeyboardInputExample {
public static void main(String[] args) {
// 创建一个Scanner对象,它会从System.in(键盘)读取数据
Scanner inputScanner = new Scanner(System.in);
System.out.print("请输入你的名字:");
// 使用nextLine()方法读取一整行文本(包括空格)
String name = inputScanner.nextLine();
System.out.println("你好," + name + "!");
System.out.print("请输入你的年龄:");
// 使用nextInt()方法读取一个整数
// 注意:这里可能会遇到一个经典的“换行符”问题,后面会提到
int age = inputScanner.nextInt();
System.out.println("你的年龄是:" + age + "岁。");
// 如果紧接着nextInt()或nextDouble()等方法后想读取一行文本,
// 需要先“吃掉”nextInt()留下的那个换行符。
// 不然下一个nextLine()会直接读到这个空换行符,导致跳过输入。
inputScanner.nextLine(); // 消耗掉nextInt()之后剩下的换行符
System.out.print("你最喜欢的水果是什么?");
String favoriteFruit = inputScanner.nextLine();
System.out.println("哦,你喜欢" + favoriteFruit + "。");
// 使用完毕后,务必关闭Scanner对象,释放系统资源。
// 这是一个好习惯,避免资源泄露。
inputScanner.close();
System.out.println("程序结束。");
}
}这段代码展示了如何读取字符串和整数。
Scanner
nextDouble()
nextBoolean()
Scanner
nextInt()
\n
nextLine()
inputScanner.nextLine();
立即学习“Java免费学习笔记(深入)”;
你可能会好奇,既然有
System.in
Scanner
System.in.read()
答案是,
System.in
InputStream
System.in.read()
'1'
'2'
'3'
所以,我们通常不会直接操作
System.in
InputStreamReader
BufferedReader
Scanner
Scanner
当然有!在
Scanner
BufferedReader
InputStreamReader
Scanner
来看一个
BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; // 这个是关键,将字节流转换为字符流
public class BufferedReaderInputExample {
public static void main(String[] args) {
// 使用try-with-resources确保BufferedReader在不再需要时自动关闭
// 这是处理资源的好习惯,可以避免忘记关闭流导致的资源泄露
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
System.out.print("请输入你的城市:");
String city = reader.readLine(); // BufferedReader只能读取一行文本
System.out.println("你来自:" + city + "。");
// 注意:BufferedReader没有直接读取整数或浮点数的方法。
// 如果需要数字,你需要先读取字符串,然后手动进行类型转换。
System.out.print("请输入你的邮政编码:");
String zipCodeStr = reader.readLine();
try {
int zipCode = Integer.parseInt(zipCodeStr); // 将字符串转换为整数
System.out.println("你的邮政编码是:" + zipCode + "。");
} catch (NumberFormatException e) {
System.err.println("邮政编码格式不正确,请输入数字!");
}
} catch (IOException e) {
// 处理可能发生的IO异常,比如输入输出错误
System.err.println("读取输入时发生错误:" + e.getMessage());
}
}
}Scanner
BufferedReader
Scanner
int
double
boolean
BufferedReader
BufferedReader
readLine()
read()
Integer.parseInt()
IOException
BufferedReader
我个人在写一些小型工具或者练习代码时,更偏爱
Scanner
BufferedReader
在使用Java进行键盘输入时,有些小“坑”是新手常遇到的,理解它们并掌握一些最佳实践能让你少走很多弯路。
nextLine()
nextInt()
nextDouble()
next()
Scanner
\n
nextLine()
nextLine()
解决方案: 在
nextInt()
nextDouble()
scanner.nextLine();
InputMismatchException
nextInt()
Scanner
InputMismatchException
解决方案:
hasNextX()
scanner.hasNextInt()
scanner.hasNextDouble()
try-catch
try
catch
InputMismatchException
// 示例:处理InputMismatchException
System.out.print("请输入一个数字:");
if (inputScanner.hasNextInt()) {
int num = inputScanner.nextInt();
System.out.println("你输入的是:" + num);
} else {
System.out.println("输入无效,请确保输入的是一个整数!");
inputScanner.next(); // 消耗掉错误的输入,避免死循环
}
inputScanner.nextLine(); // 消耗掉换行符忘记关闭资源:
Scanner
BufferedReader
解决方案:
Scanner
scanner.close();
BufferedReader
try-with-resources
try
finally
最佳实践:
总的来说,处理用户输入是一个与用户交互的关键环节。它不仅仅是技术上的实现,更是用户体验设计的一部分。一个健壮、友好的输入处理机制,能让你的程序更受欢迎。
以上就是java怎样用System.in获取键盘输入 java输入语句的基础教程的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号