gpt4 book ai didi

java - 想要创建 if 语句,然后是几个 else-if 语句,最后是一个 "capture-all"else-语句

转载 作者:行者123 更新时间:2023-11-30 05:44:14 27 4
gpt4 key购买 nike

创建一个“海盗对话”,可以选择左手或右手。我希望它对“左”和“右”的不同拼写做出积极的回答(正如您将在代码中看到的那样),但是,当我为所有非“右”或“左”的输入添加最终的“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;
}

}

“返回答案;”行是以红色下划线结尾的。如果删除最后一个“else”语句,红色下划线就会消失。

最佳答案

这是因为在所有 if 语句之后,您有一个 return 语句,如果没有任何 if 语句,该语句将永远不会被执行语句匹配,它将转到 else 语句并在那里终止。

如果不满足任何条件,您可以删除 return 行或最后的 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?!";
}
...

此外,您似乎为单词的不同字母大小写(rightRIGHT)返回相同的值,可以通过比较变量的小写值。然后,当您有多个这样的 if 时,您可以使用 switch 语句来简化:

...
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?!";
}
}
...

关于java - 想要创建 if 语句,然后是几个 else-if 语句,最后是一个 "capture-all"else-语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55128661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com