如何将具有多个变量和多个条件的 if 语句制作成 switch case?可以 && 比较案例吗?

How to make an if statement with multiple variables and multiple conditions into a switch case ? Can && compare cases?

我正在为学校制作一款剪刀石头布游戏,我有一个可以运行的游戏,但它有 213 行长。我试图通过添加 switch case 代替我的 if 语句来缩短它。是否存在某些情况下 switch case 无法正常工作而必须是 if 语句?

String weaponChoice = keyboard.nextLine();
switch (weaponChoice) {
    case "Rock":
        System.out.println("You Chose Rock");
        break;
    case "Paper":
        System.out.println("You Chose Paper");
        break;
    case "Scissors":
        System.out.println("You chose Scissors");
        break;
    default:
        System.out.println(weaponChoice + " is not a valid answer the computer gets a point!");
        compScore++;
        break;
}

我在 if 语句中有上面的代码,并且可以毫无问题地切换它,但是下面的代码我不确定如何切换它。

String getComputerChoiceVariable = getComputerChoice();
System.out.println("The computer chose " + getComputerChoiceVariable);

if (weaponChoice.equals(getComputerChoiceVariable)) {
    System.out.println("It's a draw!");
    ties++;
} else if ((weaponChoice.equals("Rock")) && (getComputerChoiceVariable.equals("Scissors"))) {
    System.out.println("You won!");
    userScore++;
} else if ((weaponChoice.equals("Paper")) && (getComputerChoiceVariable.equals("Rock"))) {
    System.out.println("You won!");
    userScore++;
} else if ((weaponChoice.equals("Scissors")) && (getComputerChoiceVariable.equals("Paper"))) {
    System.out.println("You won!");
    userScore++;
} else if (weaponChoice.equals("Rock") && getComputerChoiceVariable.equals("Paper")) {
    System.out.println("You Lost!");
    compScore++;
} else if (weaponChoice.equals("Paper") && getComputerChoiceVariable.equals("Scissors")) {
    System.out.println("You Lost!");
    compScore++;
} else if (weaponChoice.equals("Scissors") && getComputerChoiceVariable.equals("Rock")) {
    System.out.println("You Lost!");
    compScore++;
}

也许像

switch (weaponChoice,getComputerChoiceVariable){
    case "Rock",case "Scissors":
        System.out.println("You won!");
}

但是 Java 不喜欢我得到一大堆错误。一个开关可以带两个参数吗? 我是 Java 的超级新手。 我能以某种方式使用 && 来比较案例吗?

一条switch语句只能测试一个表达式的值。可以嵌套 switch 语句,但这过于冗长并且并不比一系列 if/else 语句好。

顺便说一句,您可能想要验证用户选择的武器,这样就不会有人选择 Lizard、Spock、Foo 或其他意想不到的东西。

您可以将字符串连接成一个,以便对其进行测试。我会使用 "|" 等分隔符来明确选择。另外,不同case逻辑相同,避免重复。

switch(weaponChoice + "|" + computerChoice)
{
    case "Rock|Scissors":
    case "Scissors|Paper":
    case "Paper|Rock":
        System.out.println("You won!");
        userScore++;
        break;
    case "Rock|Paper":
    case "Paper|Scissors":
    case "Scissors|Rock":
        System.out.println("You lost!");
        compScore++;
        break;
    default:
        System.out.println("It's a draw!");
        ties++;
}

简洁明了,将所有类似的案例都列在一起,没有重复。

开关块不能写入两个参数。来自 Oracle 的 documentation

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

但是,您可以通过编写以下内容使您的程序更加模块化:

if (weaponChoice.equals(getComputerChoiceVariable)) {
  System.out.println("It's a draw!");
  ties++;
} else if (userWins(weaponChoice, getComputerChoiceVariable)) {
  System.out.println("You won!");
  userScore++;
} else {
  System.out.println("You Lost!");
  compScore++;
}

并使用类似的东西:

public boolean userWins(String userWeapon, String compWeapon) {
  if((userWeapon.equals("Rock")) && (compWeapon.equals("Scissors"))) {
      return true;
  } else if((userWeapon.equals("Paper")) && (compWeapon.equals("Rock"))){
      return true;
  } else if((userWeapon.equals("Scissors")) && (compWeapon.equals("Paper"))){
      return true;
  } else if(userWeapon.equals("Rock") && compWeapon.equals("Paper")) {
      return false;
  } else if(userWeapon.equals("Paper") && compWeapon.equals("Scissors")) {
      return false;
  } else {
      return false;
  }
}

您不一定需要更改代码来缩短它。可以把树的选项放在一个列表中,将用户的选择和计算机选择后的值进行比较,看用户是否赢了。

如果你有列表[Rock, Paper, Scissors],电脑选择摇滚。找到它后面的元素,所以在这种情况下是 Paper。现在你只需要看看那个值是否等于用户的选择,如果是这样,用户就赢了。

List<String> choices = Arrays.asList("Rock", "Paper", "Scissors");

if (playerChoice.equals(computerChoice)) {
    System.out.println("Draw!");
} else if (playerChoice.equals(choices.get((choices.indexOf(computerChoice) + 1) % 3))) {
    System.out.println("You won!");
} else {
    System.out.println("You lost!");
}

这里 choices.indexOf(computerChoice) 查找计算机选择在列表中的当前位置。它添加一个来查找列表中的下一个元素,并使用余数运算符 (% 3) 来确保如果下一个索引高于列表中的最后一个元素,它会翻转回第一个元素。