Java 要创建if语句,然后是几个其他if语句,最后是一个";“捕获全部”;else语句
创造一个“海盗对话”,选择左手或右手。我希望它对“left”和“right”的不同拼写做出肯定的回答(正如您将在代码中看到的),但是,当我为所有不是“right”或“left”的输入添加最后的“else”代码时,它会给我一个“java.lang.Error”,这是无法访问的代码。在添加最后一个“else”语句之前,我测试了我的代码,它的工作方式与我所希望的一样,再次添加“else”语句,它给出了相同的错误 (我也非常感谢关于如何改进我的代码的提示和反馈,这是我完全自己创建的第二个项目) 不管怎样,代码如下:Java 要创建if语句,然后是几个其他if语句,最后是一个";“捕获全部”;else语句,java,if-statement,Java,If Statement,创造一个“海盗对话”,选择左手或右手。我希望它对“left”和“right”的不同拼写做出肯定的回答(正如您将在代码中看到的),但是,当我为所有不是“right”或“left”的输入添加最后的“else”代码时,它会给我一个“java.lang.Error”,这是无法访问的代码。在添加最后一个“else”语句之前,我测试了我的代码,它的工作方式与我所希望的一样,再次添加“else”语句,它给出了相同的错误 (我也非常感谢关于如何改进我的代码的提示和反馈,这是我完全自己创建的第二个项目) 不管怎样
package myOwn;
import java.util.Scanner;
public class myArms {
static Scanner sc2 = new Scanner(System.in);
public static void main(String[] args) {
armInput();
}
public static void armInput() {
System.out.println("Behold the arms! Which hand holds the secret item?");
String answer = sc2.nextLine();
System.out.println(armOpener(answer));
}
public static String armOpener(String answer) {
if(answer.equals("left")) {
return "Aha! Indeed the gemstone was hidden in the left hand. Now...";
}else if(answer.equals("Left")) {
return "Aha! Indeed the gemstone was hidden in the left hand. Now...";
}else if(answer.equals("LEFT")) {
return "Aha! Indeed the gemstone was hidden in the left hand. Now...";
}else if(answer.equals("right")) {
return "Bummer! The treasure was in the other hand. Easy for me to say, huh? What if there wasn't a treasure from the start of? Who knows...";
}else if(answer.equals("Right")) {
return "Bummer! The treasure was in the other hand. Easy for me to say, huh? What if there wasn't a treasure from the start of? Who knows...";
}else if(answer.equals("RIGHT")) {
return "Bummer! The treasure was in the other hand. Easy for me to say, huh? What if there wasn't a treasure from the start of? Who knows...";
}else {
return "Did you not hear me boy? I'm asking you, which hand?!";
}
return answer;
}
}
“return answer;”这一行以红色下划线结尾。如果我删除最后一个“else”语句,红色下划线就会消失。这是因为在所有
if
语句之后,您有一个return
语句,它将永远不会执行,因为如果if
语句都不匹配,它将转到else
语句并在那里终止
如果不满足任何条件,您可以根据预期的返回值删除返回
行或最终的else
...
public static String armOpener(String answer) {
String ans = answer.toLowerCase();
if (ans.equals("left")) {
return "Aha! Indeed the gemstone was hidden in the left hand. Now...";
} else if (ans.equals("right")) {
return "Bummer! The treasure was in the other hand. Easy for me to say, huh? What if there wasn't a treasure from the start of? Who knows...";
}
return "Did you not hear me boy? I'm asking you, which hand?!";
}
...
此外,您似乎正在为单词的不同字母大小写返回相同的值(right
和right
),可以通过比较变量的小写值在一条语句中进行处理。然后,当您有多个这样的if时,您可以通过使用开关
语句来简化:
...
public static String armOpener(String answer) {
switch(answer.toLowerCase()) {
case "left":
return "Aha! Indeed the gemstone was hidden in the left hand. Now...";
case "right":
return "Bummer! The treasure was in the other hand. Easy for me to say, huh? What if there wasn't a treasure from the start of? Who knows...";
default:
return "Did you not hear me boy? I'm asking you, which hand?!";
}
}
...
所以如果所有的
都不匹配,你想返回答案
,还是想返回“孩子,你没听见我说话吗?我在问你,哪只手?!”
。因为现在您的代码尝试同时执行这两项操作。无关:您可以通过调用而不是equals
来简化代码。红色下划线可能表示“无法访问的代码”由于在if
或else
块中返回后,该语句永远无法执行,因此if else
语句是详尽的,因此最后一个返回答案代码>将永远无法访问。顺便说一句,有一个equalsIgnoreCase
方法可以代替equals
,进行不区分大小写的比较。这将减少if
语句的数量。我最后只删除了“return answer;”这一行,并使用“(answer.equalsIgnoreCase)”简化了代码,这导致最终结果与您的结果有些相似,但没有使用switch语句,我还不熟悉这一语句:)@estrio Great,至少你找到了一个适合你的解决方案。