这只是一个简单的石头剪刀游戏我用eclipse作为测试,我已经通过这个网站提前一个并得到了一些答案,但现在我遇到了一个我找不到胜利者的问题?
package rockPaperScissors;
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String args[]) {
String playerChose;
String computerChose;
while(true) {
System.out.println("Welcome to rock paper scissors!");
System.out.println("Please enter \"rock\", \"paper\", or \"scissors\"");
Scanner playerChoice = new Scanner (System.in);
playerChose = playerChoice.nextLine();
Random computerChoice = new Random();
int computer = computerChoice.nextInt(3) + 1;
switch (computer) {
case 1:
computerChose = "rock";
System.out.println("Computer chose rock!");
break;
case 2:
computerChose = "paper";
System.out.println("Computer chose paper!");
break;
case 3:
computerChose = "scissors";
System.out.println("Computer chose scissors!");
}
computerChose = new String();
if (playerChose.equals("rock") && computerChose.equals("scissors") || playerChose.equals("paper") && computerChose.equals("rock") || playerChose.equals("scissors") && computerChose.equals("paper")) {
System.out.println("Player won!");
}
if(playerChose.equals(computerChose)) {
System.out.println("Game tied!");
}
if(computerChose.equals("rock") && playerChose.equals("scissors") || computerChose.equals("paper") && playerChose.equals("rock") || computerChose.equals("scissors") && computerChose.equals("paper")) {
System.out.println("Computer won!");
}
}
}
}
答案 0 :(得分:0)
将computerChose
的初始声明(在while
之前)更改为String computerChose = "";
,并删除标有computerChose = new String();
的行(在switch
之后)。< / p>
答案 1 :(得分:0)
在while
语句后,使用computerChose = newString()
重置computerChose。因此,if
语句中测试的条件都不等于true。
答案 2 :(得分:0)
经过测试并正常工作
立即初始化computerChose
,进行更改
String computerChose;
至String computerChose = "";
并删除行computerChose = new String();
,因为它在您的切换案例中初始化后将computerChoose
字符串设置为空字符串。