Java-默认开关语句

时间:2019-01-29 21:07:19

标签: switch-statement

我正在为包含switch语句的Java方法编写一些测试,但是“ default语句”似乎不起作用。我只接受:是,否,也许。其他一切应该返回也许。我的测试始终返回用户输入,与用户键入的内容无关,所以我猜我的switch语句不正确。

I have tried to move the default statement on the top

@Override
  public String choice(String ans) {
    getChoice = ans;
    switch (ans) {
      case "Yes":
        break;

      case "No":
        break;

      default:
        getChoice = "Maybe";
    }
    return getChoice;
}

Thank you!

1 个答案:

答案 0 :(得分:0)

您的开关应该可以正常工作...正如凯尔(Kyle)告诉您的:“您要覆盖什么?”

但是为什么可以执行“ if then else”切换?

Public String choice (String choice) {
   If (choice.equals("yes") || choice.equals("no") {
      return choice;
   else {
      return "maybe";
   }
}

开关适合多种选择

相关问题