我如何使用异常读取 String 以外的任何内容?
How do I read nothing else but a String using exceptions?
你能帮帮我吗?我在做一个简单的问答游戏,在游戏过程中要求用户输入他的答案。它要么是 A、B 要么是 C。我想用 try/catch 例外覆盖它...
我想让这段代码做的是,每当他输入字符串以外的内容时抛出异常(强制用户再次输入答案)。
这是代码的一部分
Scanner sc = new Scanner(System.in);
String answer = "";
boolean invalidInput = true;
while(invalidInput){
try {
answer = sc.nextLine().toUpperCase();
invalidInput = false;
}
catch(InputMismatchException e){
System.out.println("Enter a letter please");
invalidInput = true;
}
}
现在的问题是,如果我输入一个整数,它不会抛出任何东西。
谢谢
The problem now is, that if I enter an integer, it won't throw
anything.
不,问题是你认为它是一个整数,它实际上是一个字符串。
String s=1; //Gives Compilation Error
同时
String s="1"; // will not give any Error/Exception and this is your case
用户将提供输入,直到它符合您的预期输入列表,如下所示:
List<String> expectedInputs=Arrays.asList("A","B","C","D");
String input=takeInputFromUser();
if(expectedInputs.contains(input)){
//doWhatever you want to do
}else{
// throw any Exception
}
如果数据不符合你的预期,直接抛出一个InputMismatchException
。
Scanner sc = new Scanner(System.in);
String answer = "";
boolean invalidInput = true;
while(invalidInput){
try {
answer = sc.nextLine().toUpperCase();
if (!answer.equals("A") && !answer.equals("B") && !answer.equals("C")) {
throw new InputMismatchException();
}
invalidInput = false;
} catch (InputMismatchException e) {
System.out.println("Enter a letter please");
invalidInput = true;
}
}
请注意,没有必要为此类控件抛出异常。可以直接在if代码中处理报错信息
我建议你在这种情况下使用regex
try {
answer = sc.nextLine().toUpperCase();
invalidInput = !answer.matches("[ABC]");
} catch(InputMismatchException e){
System.out.println("Enter a letter please");
invalidInput = true;
}
你能帮帮我吗?我在做一个简单的问答游戏,在游戏过程中要求用户输入他的答案。它要么是 A、B 要么是 C。我想用 try/catch 例外覆盖它...
我想让这段代码做的是,每当他输入字符串以外的内容时抛出异常(强制用户再次输入答案)。 这是代码的一部分
Scanner sc = new Scanner(System.in);
String answer = "";
boolean invalidInput = true;
while(invalidInput){
try {
answer = sc.nextLine().toUpperCase();
invalidInput = false;
}
catch(InputMismatchException e){
System.out.println("Enter a letter please");
invalidInput = true;
}
}
现在的问题是,如果我输入一个整数,它不会抛出任何东西。
谢谢
The problem now is, that if I enter an integer, it won't throw anything.
不,问题是你认为它是一个整数,它实际上是一个字符串。
String s=1; //Gives Compilation Error
同时
String s="1"; // will not give any Error/Exception and this is your case
用户将提供输入,直到它符合您的预期输入列表,如下所示:
List<String> expectedInputs=Arrays.asList("A","B","C","D");
String input=takeInputFromUser();
if(expectedInputs.contains(input)){
//doWhatever you want to do
}else{
// throw any Exception
}
如果数据不符合你的预期,直接抛出一个InputMismatchException
。
Scanner sc = new Scanner(System.in);
String answer = "";
boolean invalidInput = true;
while(invalidInput){
try {
answer = sc.nextLine().toUpperCase();
if (!answer.equals("A") && !answer.equals("B") && !answer.equals("C")) {
throw new InputMismatchException();
}
invalidInput = false;
} catch (InputMismatchException e) {
System.out.println("Enter a letter please");
invalidInput = true;
}
}
请注意,没有必要为此类控件抛出异常。可以直接在if代码中处理报错信息
我建议你在这种情况下使用regex
try {
answer = sc.nextLine().toUpperCase();
invalidInput = !answer.matches("[ABC]");
} catch(InputMismatchException e){
System.out.println("Enter a letter please");
invalidInput = true;
}