Java 如果文本不等于所需的文本,则如何使用错误?
我的代码如下:Java 如果文本不等于所需的文本,则如何使用错误?,java,Java,我的代码如下: System.out.println("do you live in Bahrain? (yes/no)"); GCCYESORNO = input.nextLine(); if (GCCYESORNO.equalsIgnoreCase("NO")) { System.out.println("sorry we don't have your country information"); throw new Error("sorry we don't have y
System.out.println("do you live in Bahrain? (yes/no)");
GCCYESORNO = input.nextLine();
if (GCCYESORNO.equalsIgnoreCase("NO"))
{
System.out.println("sorry we don't have your country information");
throw new Error("sorry we don't have your country information");
}
else if (GCCYESORNO.equalsIgnoreCase("yes"));
如果某人键入的答案与“是”或“否”不同,则他/她将出现错误,我希望在此代码中保留一些内容。好吧,您似乎只是在询问一个if-else if-else案例:
然后,在每种情况下都可以执行任何操作,抛出异常?使用和else条件作为catchall
Your Code ... else { throw new IllegalArgumentException(); }
if (condition1) {
block of code to be executed if condition1 is true
} else if (condition2) {
block of code to be executed if the condition1 is false and condition2 is true
} else {
block of code to be executed if the condition1 is false and condition2 is false
}
您可以使用开关代替
switch (var.toLowerCase()) {
case "yes":
break;
case "no":
break;
default:
break;
}
在默认情况下,您拥有所有其他单词。只需按照else throw new IllegalArgumentException;添加一些内容即可;。阅读java命名约定。只有常量都是大写的;正态变量在这种情况下不起作用。
switch (var.toLowerCase()) {
case "yes":
break;
case "no":
break;
default:
break;
}