简单游戏的java if语句中的错误java输出代码

fruv7luv  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(393)

我要做一个石头剪刀布游戏,包括多个类和方法。其中一个课程是建立一个计算机播放器,它应该生成一个随机选择,然后看看它是否赢了( didIWin 方法)玩家的选择。我测试了电脑的随机选择和玩家选择的石头、布和剪刀,这会告诉你你的方法是否正确。我的输出是给我相同的答案,这三个我不知道我做错了什么。它必须在房间里 didIWin 方法,我想。任何帮助都将不胜感激。
这是我的密码:

public class Computer
    {
    //instance / member variables
    private String choice;
    private int choiceNumber;

    public Computer()
    {
        //call random set Choice
        randomSetChoice();
    }

    public String getChoice()
    {
        return "" + choice;
    }

    public void randomSetChoice()
    {
        //use Math.random()
        int min = 1;
        int max = 4;
        choiceNumber = (int)(Math.floor(Math.random() * (max - min) + min));

        //use switch case
        switch(choiceNumber)
        {
            case 1: choice =  "rock"; break; 
            case 2: choice =  "paper"; break;
            case 3: choice = "scissors"; break;
            default: choice = "invalid input...try again"; 
        }
    }   

    /*
    didIWin(Player p) will return the following values
    0 - both players have the same choice
    1 - the computer had the higher ranking choice
    -1 - the player had the higher ranking choice
     */ 
    public int didIWin(Player p)
    {
        String pc = p.getChoice();
        if(choice == "rock")
        {
            if(pc == "rock")
            {
                return 0; 
            }
            else if(pc == "paper")
            {
                return -1;
            }
            else
            {
                return 1; 
            }
        }

        else if(choice == "paper")
        {
            if(pc == "paper")
            {
                return 0;
            }
            else if(pc == "scissors")
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }

        else
        {
            if(pc == "scissors")
            {
                return 0;
            }
            else if(pc == "rock")
            {
                return -1;
            }
            else
            {
                return 1;
            }     
        }

    }

    public String toString()
    {
        return "" + "Computer chose " + choice + "!";
    }          
    }
xfyts7mz

xfyts7mz1#

实际上,如果你测试这个方法 didWin() 真管用!
我试着用这个简单的例子:

public class Main {
        static private String str = "scissors";
        static private String choice = "paper";
        static int didWin(){
            String pc = str;
            if(choice == "rock"){
                if(pc == "rock"){
                    return 0; 
                }
                else if(pc == "paper"){
                    return -1;
                }
                else{
                    return 1; 
                }
            }
            else if(choice == "paper"){
                if(pc == "paper"){
                    return 0;
                }
                else if(pc == "scissors"){
                    return -1;
                }
                else{
                    return 1;
                }
            }
            else {
                if(pc == "scissors"){
                    return 0;
                }
                else if(pc == "rock"){
                    return -1;
                }
                else {
                    return 1;
                }     
            }

        }
        public static void main(String[] args) {
            System.out.println(didWin());
        }
    }

所有的尝试都成功了。
注意你写的 didIWin() 而不是 didWin() 所以也许这就是问题所在。
也许你可以用 Player 还有你的一些尝试。

相关问题