如何为Rock,Paper,Scissor GUI游戏更正我的代码?

时间:2020-02-08 11:17:42

标签: java user-interface if-statement

我对Java编程还很陌生,我想尝试做一个GUI App,现在我被if / else语句机制所困扰。到目前为止,除了“ Rock Bet”和“ Paper Bet”机制外,其他所有内容都可以运行。我已经测试过剪刀式赌注,并且一切正常。

这里的问题是,每当计算机显示内容时,它都会通过if / else语句进行选择和运行,因此判决总是错误的。 “ Rock Bet”和“ Paper Bet”与剪刀式有何不同?

(我曾尝试将“ Scissor Bet” if语句机制放在顶部,在Rock and Paper if / else语句之前,并以某种方式使判决错误。)

>
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class gameRPS extends JFrame {
    private JPanel mainPanel;
    private JLabel textLabel1;
    private JButton rockButton;
    private JButton paperButton;
    private JButton scissorButton;
    private JLabel textLabel2;
    private JLabel computersLabel;
    private JLabel userBet;
    private JButton enterButton;
    private JLabel verdictLabel;

    public gameRPS(String title) {
        super(title);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(mainPanel);
        this.pack();

        rockButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                userBet.setText("YOU PICKED: ROCK!");
            }
        });
        paperButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                userBet.setText("YOU PICKED: PAPER!");
            }
        });
        scissorButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                userBet.setText("YOU PICKED: SCISSOR!");
            }
        });

        String[] rpsChoices = {"ROCK!", "PAPER!", "SCISSOR!"};
        Random rand = new Random();
        int computerBet = rand.nextInt(rpsChoices.length);

        enterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                String computersChoice = rpsChoices[computerBet];
                String userBetRock = "ROCK!";
                String userBetPaper = "PAPER!";
                String userBetScissor = "SCISSOR!";

                computersLabel.setText(computersChoice); //Displays the Computer's Choice

        //Mechanics for the Rock Bet
                if (userBetRock == "ROCK!" && computersChoice.equals("ROCK!")) {
                    verdictLabel.setText("DRAW");
                }
                else if (userBetRock == "ROCK!" && computersChoice.equals("PAPER!")) {
                    verdictLabel.setText("YOU LOSE");
                }
                else if (userBetRock == "ROCK!" && computersChoice.equals("SCISSOR!")) {
                    verdictLabel.setText("YOU WIN");
                }
        //Mechanics for the Paper Bet
                if (userBetPaper == "PAPER!" && computersChoice.equals("PAPER!")) {
                    verdictLabel.setText("DRAW");
                }
                else if (userBetPaper == "PAPER!" && computersChoice.equals("ROCK!")) {
                    verdictLabel.setText("YOU WIN");
                }
                else if (userBetPaper == "PAPER!" && computersChoice.equals("SCISSOR!")) {
                    verdictLabel.setText("YOU LOSE");
                }
        //Mechanics for the Scissor Bet (WORKING)
                if (userBetScissor == "SCISSOR!" && computersChoice.equals("SCISSOR!")) {
                    verdictLabel.setText("DRAW");
                }
                else if (userBetScissor == "SCISSOR!" && computersChoice.equals("ROCK!")) {
                    verdictLabel.setText("YOU LOSE");
                }
                else if (userBetScissor == "SCISSOR!" && computersChoice.equals("PAPER!")) {
                    verdictLabel.setText("YOU WIN");
                }
            }
        });

    }
    public static void main(String[] args) {
        JFrame frame = new gameRPS("Rock Paper Scissors!");
        frame.setSize(300,320);
        frame.setResizable(true);
        frame.setVisible(true);

    }
}

1 个答案:

答案 0 :(得分:1)

在Java中,您无法像在这里那样用==检查字符串是否相等:

if (userBetPaper == "PAPER!" && ...) ...

以及所有其他if中,您必须使用if来完成在.equals()的第二种情况下的操作:

if (userBetPaper.equals("PAPER!") && ...) ...

但是,例如,我认为您所做的工作也存在逻辑错误

String userBetRock = "ROCK!";
...
if (userBetRock == "ROCK!" &&...)...

始终为真,您永远不会更改它,也永远不会使用用户输入:例如,在按钮的ActionEvent侦听器上,您可以在类中的String中保存什么?他选择了,然后在enterButton动作监听器中检查String包含的内容