1

我创建了一个做纸石剪刀的课程。

public class RPS {

    private char cAns;

    public RPS()
    {
        reset();
    }

    public String promptShoot()
    {
        return "Rock, Paper, Scissors, Shoot! (r/p/s)\n";
    }

    public void AI()
    {
        double temp = Math.random();
        int num = (int)(temp * 2.99);

        switch(num) 
        {
            case 0: 
                cAns = 'r';
                break;
            case 1:
                cAns = 'p';
                break;
            case 2:
                cAns = 's';
                break;
        }
    }

全班随机选择电脑。然后它使用扫描仪要求人工输入。

    public int shoot(char hAns)
    {
        if(hAns == cAns)
            return 0;
        else if((cAns == 'r' && hAns == 'p')
                || (cAns == 'p' && hAns == 's')
                || (cAns == 's' && hAns == 'r'))
            return 1;
        else
            return -1;
    }

一旦双方都选择了他们的选择,它就会宣布获胜者:

    public String winner(int won)
    {
        if(won == 1)
            return "The human won!!! All hail the human!!!";
        else if(won == -1)
            return "The computer won!!! Humans must die!!!";
        else
            return "Tie!";
    }

    public void reset()
    {
        cAns = 'a';
    }
}

我认为这是可行的,但我没有意识到我需要创建一个单独的类来运行这个 RPS 类。

到目前为止我有这个

import java.util.Scanner;

public class game
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);

        RPS choice = new RPS();

        choice.AI();

        System.out.print(choice.promptShoot());
        choice.hAns(scan.nextInt());

        scan.close();
    }
}

我认为这会初始化游戏,但它不起作用。有什么建议或者我可以指出正确的方向吗?

4

1 回答 1

0

添加staticwinnershoot方法。把它放在最后:

System.out.println(winner(shoot(scan.nextLine().charAt(0))));

消除choice.hAns(scan.nextInt());

于 2013-03-30T18:47:45.577 回答