Java逻辑问题

时间:2013-12-10 22:43:43

标签: java swing

我刚拿到我的摇滚,纸,剪刀,蜥蜴,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!");
                }
            }
        });
    }
}

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

1 个答案:

答案 0 :(得分:5)

您必须嵌套if-else语句,如下所示:

if (P1Rock.isSelected()) {
   if (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 (P1Scissors.isSelected()) {
   if (P2Scissors.isSelected()) {
     ... // and so on

因为你首先要证明Player1选择了什么。然后你要检查它是如何适合其他玩家选项的。


此外,还有一些提示可以让您编写更清晰的代码:

  • 在实现用户界面(UI)之前实现您的逻辑。所以逻辑从UI中断了。之后更改逻辑会更容易。
  • 由于Java是面向对象编程(OOP)语言,因此请考虑使用实现接口或扩展抽象类的RockScissors,(等等)类。

这可能会产生更多代码,但如果你有一种新的选择(比如 Chuck ),你只需要添加一个新类Chuck并实现殴打针对其他课程的可能性(例如RockScissors,...)。

UI也是如此。你应该将你的组件清理成一些数组(按钮[0 - 4]),所以你don't have to repeat yourself使用一些循环条件(最后,你也可以更容易地将 Chuck 添加到你的军队)。