如何让 java 中的 Scanner 读取字符串?
How do I get the Scanner in java to read a string?
我如何让我的程序在用户输入 q 时退出?
扫描仪有问题吗?
我的代码
import java.util.*;
public class Main{
public static void main(String []args){
int age;
Scanner scan = new Scanner(System.in);
System.out.println("Enter your age, or enter 'q' to quit the program.");
age = scan.nextInt();
if(age.equals("q") || age.equals("Q")){
return 0;
}
System.out.println("Your age is " + age);
}
}
我在您的代码中主要看到两个问题:
- 它缺少一个循环来重复询问年龄。可以有很多方法(
for
、while
、do-while
)来编写循环,但我发现 do-while
最适合这种情况,因为它总是执行循环中的语句do
至少阻止一次。
age
是 int
类型,因此不能与字符串进行比较,例如您的代码 age.equals("q")
不正确。处理这种情况的一个好方法是将输入输入到一个 String
类型的变量中,并检查该值是否应该 allow/disallow 处理它(例如,尝试将其解析为 int
).
请注意,当您尝试将无法解析为 int
(例如 "a"
)的字符串解析时,您会得到一个需要处理的 NumberFormatException
(例如显示错误消息、更改某些状态等)。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int age;
String input;
Scanner scan = new Scanner(System.in);
boolean valid;
do {
// Start with the assumption that input will be valid
valid = true;
System.out.print("Enter your age, or enter 'q' to quit the program: ");
input = scan.nextLine();
if (!(input.equals("q") || input.equals("Q"))) {
try {
// Try to parse input into an int
age = Integer.parseInt(input);
System.out.println("Your age is " + age);
} catch (NumberFormatException e) {
System.out.println("Invalid input");
// Change the value of valid to false
valid = false;
}
}
} while (!valid || !(input.equals("q") || input.equals("Q")));
}
}
样本运行:
Enter your age, or enter 'q' to quit the program: a
Invalid input
Enter your age, or enter 'q' to quit the program: 12.5
Invalid input
Enter your age, or enter 'q' to quit the program: 14
Your age is 14
Enter your age, or enter 'q' to quit the program: 56
Your age is 56
Enter your age, or enter 'q' to quit the program: q
我如何让我的程序在用户输入 q 时退出? 扫描仪有问题吗?
我的代码
import java.util.*;
public class Main{
public static void main(String []args){
int age;
Scanner scan = new Scanner(System.in);
System.out.println("Enter your age, or enter 'q' to quit the program.");
age = scan.nextInt();
if(age.equals("q") || age.equals("Q")){
return 0;
}
System.out.println("Your age is " + age);
}
}
我在您的代码中主要看到两个问题:
- 它缺少一个循环来重复询问年龄。可以有很多方法(
for
、while
、do-while
)来编写循环,但我发现do-while
最适合这种情况,因为它总是执行循环中的语句do
至少阻止一次。 age
是int
类型,因此不能与字符串进行比较,例如您的代码age.equals("q")
不正确。处理这种情况的一个好方法是将输入输入到一个String
类型的变量中,并检查该值是否应该 allow/disallow 处理它(例如,尝试将其解析为int
).
请注意,当您尝试将无法解析为 int
(例如 "a"
)的字符串解析时,您会得到一个需要处理的 NumberFormatException
(例如显示错误消息、更改某些状态等)。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int age;
String input;
Scanner scan = new Scanner(System.in);
boolean valid;
do {
// Start with the assumption that input will be valid
valid = true;
System.out.print("Enter your age, or enter 'q' to quit the program: ");
input = scan.nextLine();
if (!(input.equals("q") || input.equals("Q"))) {
try {
// Try to parse input into an int
age = Integer.parseInt(input);
System.out.println("Your age is " + age);
} catch (NumberFormatException e) {
System.out.println("Invalid input");
// Change the value of valid to false
valid = false;
}
}
} while (!valid || !(input.equals("q") || input.equals("Q")));
}
}
样本运行:
Enter your age, or enter 'q' to quit the program: a
Invalid input
Enter your age, or enter 'q' to quit the program: 12.5
Invalid input
Enter your age, or enter 'q' to quit the program: 14
Your age is 14
Enter your age, or enter 'q' to quit the program: 56
Your age is 56
Enter your age, or enter 'q' to quit the program: q