【问题标题】:Rock, Scissors, Paper石头、剪刀、纸
【发布时间】:2014-11-03 10:42:29
【问题描述】:

我刚做了一个石头、剪刀、纸游戏,到目前为止已经走了一点路。我真的不知道(尝试了一段时间)当用户或计算机首先达到 3 分时让游戏停止。

我有一些想法来做一个 if 语句 == 3 = 获胜。但如果我这样做,那么按钮也应该被禁用..我在想类似的事情,但我不能再明白了!所以想法会很棒!

public void actionPerformed(ActionEvent e) {
        int Choice= 0;

        if(e.getSource() == exit) {
            this.dispose();
            System.exit(0); 
        }
        else if(e.getSource() == newgame) {
            this.dispose();
            new RockScissorsPaper();
        }
        else if (e.getSource() == Rock) {
            Choice= Rock;       
        }
        else if (e.getSource() == Scissors) {
            Choice= Scissors;
        }
        else {
            Choice= Paper; 
        }
        calculate(Choice);

    }//actionPerformed


     / ** 
     * Computer game selection is randomized and compared with the user 
     * Choice (method arguments). The game result is stored and displayed 
     * UI. 
     * / 
    public void calculate(int val){

        //Get user choise
        UserChoose= val;     

        // randomise the computer's choice and typing on the integer
        ComputerChoose = (int)(Math.random() * 3);

        //show result

        resultatLabel.setHorizontalAlignment(0); 
        infoLabel.setHorizontalAlignment(0);    
        infoLabel.setText("Result round " + ++round +":");  

        if (UserChoose == ComputerChoose)
            resultatLabel.setText("Tie!");   

        else if (UserChoose == Rock &&  ComputerChoose == Scissors || 
                UserChoose == Scissors &&  ComputerChoose == Paper||
                UserChoose == Paper &&  ComputerChoose == Rock) {
            resultatLabel.setText("You win! One more game?");
            användarResultat.setText(" Your wins: " + ++UserWins+ "st");
        }
        else {
            resultatLabel.setText(" You Lose! Try again!");
            datorResultat.setText(" Computer wins: " + ++computerWins + "st");
        }
    }

    public static void main(String[] args) {
        new RockScissorsPaper();
    }


}

【问题讨论】:

  • 请将这段代码翻译成英文并只粘贴必要的代码,而不是整个程序。
  • 好的,会的!等等。

标签: java jpanel


【解决方案1】:

只需将属性添加到您的Stensaxpaseclass,例如

private int numberOfWinsUser;
private int numberOfWinsComputer;

每次有人获胜(berakna)时,您都会更新他们并检查是否有人达到了 3 次获胜。由于此事件驱动,因此无需使用循环。

【讨论】:

  • 嗯。我有点走神了。我不明白你的意思,但我仍然有点迷失我应该怎么做。我的想法是这样的。 if numberOfWinsUser == 3 { "你赢了" else (numberOfWinsComputer == 3 { "你输了"
  • @Barry 标准结构是while (!endOfTheGame){ play the game } handle end of the game
  • 我想多了。如果我现在使用“第一个达到 3 次获胜者,赢得游戏”来执行 If 语句,但问题是,我如何禁用按钮以使其不起作用?
  • 有谁能帮帮我吗?
猜你喜欢
  • 1970-01-01
  • 2020-06-20
  • 2013-12-09
  • 1970-01-01
  • 2014-11-21
  • 2015-01-15
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多