當我運行這個時,它顯示案例中的所有System.out.println("")
陳述。我如何使它選擇正確的if
聲明,而不是別的?Rock Paper Scissor如果陳述不起作用
如果在找到結果後我可以回到起點,那也是有幫助的。
import java.util.Scanner;
import java.util.Random;
public class TEST {
private static Scanner myScanner;
public static void main(String[] args) {
myScanner = new Scanner(System.in);
Random myRandom = new Random();
int randomNumber;
char reply;
System.out.print("Rock(R) Paper(P) or Scissors(S)? ");
reply = myScanner.findWithinHorizon(".", 0).charAt(0);
randomNumber = myRandom.nextInt(3) + 1;
switch (randomNumber) {
case 1:
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Rock. You lost!");
}
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Rock. You tied!");
}
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Rock. You won!");
break;
}
case 2:
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Paper. You tied!");
}
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Paper. You won!");
}
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Paper. You lost!");
break;
}
case 3:
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Scissor. You won!");
}
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Scissor. You lost!");
}
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Scissor. You tied!");
break;
}
}
}
}
**獎勵練習:**想想如何進一步簡化程序,避免不必要的重複。使用'Character.toLowerCase',並將數值分配給用戶的輸入,以便像減法這樣的操作可以幫助您輕鬆確定贏/失狀態。 – ADTC