Scanner
Scanner
类是 Java 中一个非常常用的工具类,用于获取用户输入、读取文件、解析字符串等。让我们系统地学习一下它的用法吧!🌟
🌱 1. 导入 Scanner 类
Scanner
类属于 java.util
包,需要先导入:
1
| import java.util.Scanner;
|
📝 2. 创建 Scanner 对象
使用 System.in
创建一个 Scanner
对象,从控制台获取输入:
1
| Scanner scanner = new Scanner(System.in);
|
💬 3. 常用方法
📌 3.1 接收不同类型输入
方法 |
数据类型 |
示例 |
nextLine() |
String |
scanner.nextLine() |
next() |
String (无空格) |
scanner.next() |
nextInt() |
int |
scanner.nextInt() |
nextDouble() |
double |
scanner.nextDouble() |
nextFloat() |
float |
scanner.nextFloat() |
nextLong() |
long |
scanner.nextLong() |
nextBoolean() |
boolean |
scanner.nextBoolean() |
🌰 示例:读取不同类型的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.util.Scanner;
public class ScannerExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.print("请输入你的名字: "); String name = scanner.nextLine();
System.out.print("请输入你的年龄: "); int age = scanner.nextInt();
System.out.print("请输入你的身高 (米): "); double height = scanner.nextDouble();
System.out.println("你好, " + name + ",年龄: " + age + ",身高: " + height + "米");
scanner.close(); } }
|
🖥️ 运行示例输出:
1 2 3 4
| 请输入你的名字: Alice 请输入你的年龄: 22 请输入你的身高 (米): 1.65 你好, Alice,年龄: 22,身高: 1.65米
|
🔍 4. 判断输入(检测是否有下一个)
方法 |
功能 |
hasNext() |
判断是否有下一个 String |
hasNextInt() |
判断是否有下一个 int |
hasNextDouble() |
判断是否有下一个 double |
hasNextBoolean() |
判断是否有下一个 boolean |
🌰 示例:判断输入类型
1 2 3 4 5 6 7 8 9 10 11
| Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个整数: "); if (scanner.hasNextInt()) { int num = scanner.nextInt(); System.out.println("你输入了整数: " + num); } else { System.out.println("这不是一个有效的整数!"); }
scanner.close();
|
🔄 5. 循环输入
🌰 示例:不断读取直到输入 exit
为止
1 2 3 4 5 6 7 8 9 10 11 12 13
| Scanner scanner = new Scanner(System.in); System.out.println("请输入文字 (输入 'exit' 退出):");
while (true) { String input = scanner.nextLine(); if ("exit".equalsIgnoreCase(input)) { System.out.println("程序已退出!"); break; } System.out.println("你输入的是: " + input); }
scanner.close();
|
⚠️ 6. 注意事项
❌ nextLine() 与其他 next() 方法混用问题
nextLine()
会读取换行符,如果前面用 nextInt()
、nextDouble()
等方法,换行符未消费,nextLine()
会直接读取这个换行符。
在Java
中,Scanner
类的nextLine()
方法用于读取一行文本,它会消费到行末的换行符。而next()
方法用于读取下一个单词,它会在遇到空格、制表符、换行符等空白字符时停止读取,但不会消费空格,只是将其作为分隔符来确定单词的边界。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.util.Scanner;
public class ScannerExample { public static void main(String[] args) { Scanner scanner = new Scanner("Hello World");
String word1 = scanner.next(); String word2 = scanner.next();
System.out.println(word1); System.out.println(word2);
scanner.close(); } }
|
输出:
❌ 错误示例:
1 2 3 4 5 6
| Scanner scanner = new Scanner(System.in); System.out.print("请输入年龄: "); int age = scanner.nextInt(); System.out.print("请输入名字: "); String name = scanner.nextLine(); System.out.println("年龄: " + age + ",名字: " + name);
|
✅ 解决方法:在 nextLine()
前加一个空的 nextLine()
来消费换行符:
1 2 3 4 5 6 7
| Scanner scanner = new Scanner(System.in); System.out.print("请输入年龄: "); int age = scanner.nextInt(); scanner.nextLine(); System.out.print("请输入名字: "); String name = scanner.nextLine(); System.out.println("年龄: " + age + ",名字: " + name);
|
✅ 7. 小练习
1️⃣ 编写一个程序,要求用户输入姓名、年龄、成绩(浮点数),并输出这些信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.util.Scanner;
public class JudegeScore {
public static void main(String[] args) { Scanner sca = new Scanner(System.in); System.out.println("请输入姓名:"); String Name = sca.nextLine(); System.out.println("请输入年龄:"); int Age = sca.nextInt(); System.out.println("请输入成绩:"); float Score = sca.nextFloat(); System.out.println("姓名:" + Name); System.out.println("年龄:" + Age); System.out.println("成绩:" + Score); sca.close(); } }
|
2️⃣ 使用 hasNextInt()
检测用户是否输入一个整数,直到输入有效数字为止。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.util.Scanner;
public class HasIntt { public static void main(String[] args) { Scanner sca = new Scanner(System.in); System.out.println("请输入一个整数:"); while (!sca.hasNextInt()){ sca.next(); } System.out.println("输入的整数是:" + sca.nextInt()); sca.close(); } }
|
3️⃣ 编写一个简单的计算器,支持加、减、乘、除操作。
Scanner
类非常强大,适用于用户交互、文件读取和数据解析。练习起来吧!💪💻 如果有问题或者需要更多例题,告诉我!🚀✨