简体   繁体   English

Java-为什么不起作用?

[英]Java - Why won't this work?

This is just a simple rock paper scissors game I made with eclipse as a test and I already put one through this website earlier and got some answers but now I've run into a problem where I cannot find a winner? 这只是我用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!");
            }
        }
    }
}

Change your initial declaration of computerChose (before your while ) to String computerChose = ""; 将您的初始computerChose声明(在while之前)更改为String computerChose = ""; and remove the line that reads computerChose = new String(); 并删除读取computerChose = new String(); (after your switch ). switch )。

After the while statement, computerChose is getting reset using computerChose = newString() . while语句之后,将使用computerChose = newString()重置computerChose = newString() Hence, none of the conditions being tested in the if statements are equating to true. 因此, if语句中测试的条件均不等于true。

Tested and works 经过测试和工作

Initialize computerChose right away, so change 立即初始化computerChose ,然后选择
String computerChose; to String computerChose = ""; String computerChose = "";

and take away the line computerChose = new String(); 并删除computerChose = new String();选择行computerChose = new String(); because it's setting the computerChoose String to an empty String after you initialized it in your switch cases. 因为在开关盒中初始化后,它会将computerChoose字符串设置为空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM