这段代码从不打印我是赢还是输,或者是平局,我无法弄清楚。我知道 hischoice 和 whichpick 都设置正确,但 ifs 似乎不起作用。我是在错误地使用字符串还是在比较它们错误?我是 Java 新手,所以我不确定我做错了什么。
import java.util.Scanner;
import java.util.Random;
public class rps {
public static void main(String[] args) {
int oppchoice;
String hischoice = "";
String whichpick;
String rock = "rock";
String paper = "paper";
String scissors = "scissors";
Scanner mS = new Scanner(System.in);
Random mR = new Random();
System.out.print("Type your choice (rock/paper/scissors): ");
whichpick = mS.nextLine();
oppchoice = mR.nextInt(3) + 1;
if (oppchoice == 1) {
hischoice = rock;
}
if (oppchoice == 2) {
hischoice = paper;
}
if (oppchoice == 3) {
hischoice = scissors;
}
System.out.println(hischoice);
if ((hischoice == rock) && (whichpick == paper)) {
System.out.println("You win! :)");
}
if ((hischoice == paper) && (whichpick == scissors)) {
System.out.println("You win! :)");
}
if ((hischoice == scissors) && (whichpick == rock)) {
System.out.println("You win! :)");
}
if ((hischoice == rock) && (whichpick == scissors)) {
System.out.println("You lose! :(");
}
if ((hischoice == paper) && (whichpick == rock)) {
System.out.println("You lose! :(");
}
if ((hischoice == scissors) && (whichpick == paper)) {
System.out.println("You lose! :(");
}
if ((hischoice == rock) && (whichpick == rock)) {
System.out.println("It's a tie! :|");
}
if ((hischoice == scissors) && (whichpick == scissors)) {
System.out.println("It's a tie! :|");
}
if ((hischoice == paper) && (whichpick == paper)) {
System.out.println("It's a tie! :|");
}
}
}