- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
创建一个“海盗对话”,可以选择左手或右手。我希望它对“左”和“右”的不同拼写做出积极的回答(正如您将在代码中看到的那样),但是,当我为所有非“右”或“左”的输入添加最终的“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?!";
}
...
此外,您似乎为单词的不同字母大小写(right
和 RIGHT
)返回相同的值,可以通过比较变量的小写值。然后,当您有多个这样的 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/
使用登录后,我想吐出用户名。 但是,当我尝试单击登录按钮时, 它给了我力量。 我看着logcat,但是什么也没显示。 这种编码是在说。 它将根据我在登录屏幕中输入的名称来烘烤用户名。 不会有任何密码。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我是一名优秀的程序员,十分优秀!