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

标签 java if-statement

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

相关文章:

java - 帮助编写您自己的 javax.swing.text.Document

java - JasperReports - 如何在 Java 中使用远程 JSON 数据报告运行报告

SQL:IF 语句 Access 2010

c++ - 用户定义类中的 Bool 作为函数 c++ 出现问题

java - 在 if 语句中比较太多变量

java - 作用域中断,它们存在吗?

java - Varags 对象处理异常

java - PendingIntent 和 AlarmManager

c if 语句检查零

javascript - 在js中有条件地分配const值