Java逻辑问题

 csc1520075 发布于 2023-02-11 16:47

我刚拿到我的摇滚,纸,剪刀,蜥蜴,Spock完成(Big Bang Theory笑话); 但当我运行它并尝试任何与蜥蜴或Spock的任何东西时,答案是没有接近正确的.这种情况发生在基本的摇滚,纸张和剪刀上,但是当我将所有逻辑改为"其他如果"语句时,它得到纠正.

import javax.swing.JFrame;
import javax.swing.JPanel;

import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.GridLayout;

public class RPS extends JFrame {

    ButtonGroup P1choices, P2choices;

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

    public RPS() {
        super("Rock, Paper, Scissors, Lizard, Spock");

        setSize(400, 400);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        final CardLayout cardLayout = new CardLayout(10, 10);
        final JPanel cardPanel = new JPanel(cardLayout);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();

        final JRadioButton P1Rock = new JRadioButton("Rock");
        final JRadioButton P1Paper = new JRadioButton("Paper");
        final JRadioButton P1Scissors = new JRadioButton("Scissors");
        final JRadioButton P1Lizard = new JRadioButton("Lizard");
        final JRadioButton P1Spock = new JRadioButton("Spock");

        final JRadioButton P2Rock = new JRadioButton("Rock");
        final JRadioButton P2Paper = new JRadioButton("Paper");
        final JRadioButton P2Scissors = new JRadioButton("Scissors");
        final JRadioButton P2Lizard = new JRadioButton("Lizard");
        final JRadioButton P2Spock = new JRadioButton("Spock");

        JButton nextButton = new JButton("Next");
        JButton finish = new JButton("Finish");

        P1choices = new ButtonGroup();
        P1choices.add(P1Rock);
        P1choices.add(P1Paper);
        P1choices.add(P1Scissors);
        P1choices.add(P1Lizard);
        P1choices.add(P1Spock);

        P2choices = new ButtonGroup();
        P2choices.add(P2Rock);
        P2choices.add(P2Paper);
        P2choices.add(P2Scissors);
        P2choices.add(P2Lizard);
        P2choices.add(P2Spock);

        final JLabel statusLabel = new JLabel(" ");
        JLabel P1turn = new JLabel("It is Player 1's turn. Choose:");
        JLabel p2turn = new JLabel("It is Player 2's turn. Choose:");

        panel1.add(P1turn);
        panel1.add(P1Rock);
        panel1.add(P1Paper);
        panel1.add(P1Scissors);
        panel1.add(P1Lizard);
        panel1.add(P1Spock);
        panel1.add(nextButton);
        panel1.setLayout(new GridLayout(7, 1));

        panel2.add(p2turn);
        panel2.add(P2Rock);
        panel2.add(P2Paper);
        panel2.add(P2Scissors);
        panel2.add(P2Lizard);
        panel2.add(P2Spock);
        panel2.add(finish);
        panel2.setLayout(new GridLayout(7, 1));

        cardPanel.add(panel1);
        cardPanel.add(panel2);

        add(statusLabel, BorderLayout.NORTH);
        add(cardPanel, BorderLayout.CENTER);

        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cardLayout.next(cardPanel);
            }
        });
        finish.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (P1Rock.isSelected() && P2Scissors.isSelected()) {
                    statusLabel.setText("Player 1 wins: Rock crushes Scissors");
                } else if (P2Paper.isSelected()) {
                    statusLabel.setText("Player 2 wins: Scissors cut Paper");
                } else if (P2Rock.isSelected()) {
                    statusLabel.setText("It's a tie!");
                } else if (P2Lizard.isSelected()) {
                    statusLabel.setText("Player 1 wins: Rock crushes Lizard");
                } else if (P2Spock.isSelected()) {
                    statusLabel.setText("Player 2 wins: Spock vaporizes Rock");
                } else if (P1Paper.isSelected() && P2Rock.isSelected()) {
                    statusLabel.setText("Player 1 wins: Rock crushes Scissors");
                } else if (P2Scissors.isSelected()) {
                    statusLabel.setText("Player 2 wins: Scissors cut Paper");
                } else if (P2Paper.isSelected()) {
                    statusLabel.setText("It's a tie!");
                } else if (P2Lizard.isSelected()) {
                    statusLabel.setText("Player 2 wins: Lizard eats Paper");
                } else if (P2Spock.isSelected()) {
                    statusLabel.setText("Player 1 wins: Paper disproves Spock");
                } else if (P1Scissors.isSelected() && P2Paper.isSelected()) {
                    statusLabel.setText("Player 1 wins: Scissors cut Paper");
                } else if (P2Rock.isSelected()) {
                    statusLabel.setText("Player 2 wins: Rock crushes Scissors");
                } else if (P2Scissors.isSelected()) {
                    statusLabel.setText("It's a tie!");
                } else if (P2Lizard.isSelected()) {
                    statusLabel.setText("Player 1 wins: Scissors decapitate Lizard");
                } else if (P2Spock.isSelected()) {
                    statusLabel.setText("Player 2 wins: Spock smashes Scissors");
                } else if (P1Lizard.isSelected() && P2Spock.isSelected()) {
                    statusLabel.setText("Player 1 wins: Lizard poisons Spock");
                } else if (P2Rock.isSelected()) {
                    statusLabel.setText("Player 2 wins: Rock crushes Scissors");
                } else if (P2Scissors.isSelected()) {
                    statusLabel.setText("Player 2 wins: Scissors decapitate Lizard");
                } else if (P2Lizard.isSelected()) {
                    statusLabel.setText("It's a tie!");
                } else if (P2Paper.isSelected()) {
                    statusLabel.setText("Player 1 wins: Lizard eats Paper");
                } else if (P1Spock.isSelected() && P2Rock.isSelected()) {
                    statusLabel.setText("Player 1 wins: Spock vaporizes Rock");
                } else if (P2Paper.isSelected()) {
                    statusLabel.setText("Player 2 wins: Paper disproves Spock");
                } else if (P2Scissors.isSelected()) {
                    statusLabel.setText("Player 1 wins: Spock smashes Scissors");
                } else if (P2Lizard.isSelected()) {
                    statusLabel.setText("Player 2 wins: Lizard poisons Spock");
                } else if (P2Spock.isSelected()) {
                    statusLabel.setText("It's a tie!");
                }
            }
        });
    }
}

很抱歉,如果代码很乱,我知道有更有效的方法可以做到这一点,但我想在我清理它之前至少让它工作.谢谢你的帮助!

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有