if语句和切换问题

时间:2014-08-11 08:08:06

标签: java swing

我正在尝试制作一个游戏,用户可以使用提供的微调器输入他们的投注,然后选择一个冷或热的单选按钮。这些单选按钮基于色轮颜色,冷色(紫色绿色蓝色)或热色(黄色,红色,橙色)。这些颜色在我的阵列中保存为图像,颜色随机选择,并显示在一个不可见的面板中,一旦你点击播放按钮就可以看到它。您的下注是根据金额加倍,三倍或四倍。如果颜色是白色或黑色,房子将赢,用户将不会收到钱,如果他们选择冷,颜色很热,反之亦然。我遇到的问题是在玩游戏时我一直在赢,即使颜色很冷,我选择了热或者如果颜色很热我选择了冷,我看不到哪里有虫子需要一些帮助。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JTextField;
import java.util.Random;
import java.util.ArrayList;

public class HotAndColdGUI extends JFrame
{
    private SpinnerModel sm = new SpinnerNumberModel(10, 10, 100, 10);
    JSpinner bet = new JSpinner(sm);
    private JButton replay = new JButton("Play Again");
    private JRadioButton Hot = new JRadioButton("Hot", false);
    private JRadioButton Cold = new JRadioButton("Cold", false);
    private JButton play = new JButton("Play");
    private JButton exit = new JButton("Exit");
    private ButtonGroup group = new ButtonGroup();
    private JPanel color = new JPanel();
    private int moneyEarned;
    private static String[] imageList = { "C:\\Users\\Joe\\HotAndColdFastGambling\\Images\\red.jpg", "C:\\Users\\Joe\\HotAndColdFastGambling\\Images\\orange.jpg", "C:\\Users\\Joe\\HotAndColdFastGambling\\Images\\yellow.jpg", "C:\\Users\\Joe\\HotAndColdFastGambling\\Images\\blue.jpg", "C:\\Users\\Joe\\HotAndColdFastGambling\\Images\\purple.jpg", "C:\\Users\\Joe\\HotAndColdFastGambling\\Images\\green.jpg", "C:\\Users\\Joe\\HotAndColdFastGambling\\Images\\white.jpg", "C:\\Users\\Joe\\HotAndColdFastGambling\\Images\\black.jpg" };

    public HotAndColdGUI(int width, int height)
    {
        super("HotAndColdGUI");

        setBounds(0, 0, width, height);
        Panel bPanel = new Panel();
        JLabel betLabel = new JLabel("Bet:");
        bPanel.add(betLabel);
        bPanel.add(bet);
        bPanel.setLayout(new FlowLayout());
        add(bPanel, BorderLayout.NORTH);
        bPanel.setVisible(true);

        Panel hcPanel = new Panel();
        Hot = new JRadioButton("Hot");
        Cold = new JRadioButton("Cold");
        group.add(Hot);
        group.add(Cold);
        hcPanel.add(Hot);
        hcPanel.add(Cold);
        hcPanel.add(play);
        hcPanel.add(replay);
        hcPanel.add(exit);
        hcPanel.setLayout(new FlowLayout());
        add(hcPanel, BorderLayout.CENTER);
        hcPanel.setVisible(true);
        play.addActionListener(new ButtonListener());
        exit.addActionListener(new ButtonListener());

        ImageIcon image;
        JLabel label = new JLabel();
        int colorNumber = (int) (8 * Math.random() + 1);
        image = new ImageIcon(imageList[colorNumber]);
        label.setIcon(image);
        color.add(label);
        JTextField winningsField = new JTextField();
        winningsField.setEditable(false);
        bet.getValue();
        String Sbet = bet.getValue() + "";
        int wager = Integer.parseInt(Sbet);
        if (wager >= 100)
        {
            moneyEarned = wager * 2;
            winningsField.setText(moneyEarned + "");

        }
        else if (wager >= 50)
        {
            moneyEarned = wager * 3;
            winningsField.setText(moneyEarned + "");

        }
        else if (wager <= 50)
        {
            moneyEarned = wager * 4;
            winningsField.setText(moneyEarned + "");

        }
        if (Hot.isSelected())
        {
            switch (colorNumber)
            {
                case 4:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
                case 5:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
                case 6:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
                case 7:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
                case 8:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
            }
        }
        if (Cold.isSelected())
        {
            switch (colorNumber)
            {
                case 1:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
                case 2:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
                case 3:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
                case 7:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
                case 8:
                    moneyEarned = wager * 0;
                    winningsField.setText(moneyEarned + "");
                    break;
            }
        }

        JLabel pwinnings = new JLabel();
        if (moneyEarned == 0)
        {
            pwinnings.setText("Sorry, You Lost!");

        }
        if (moneyEarned > 0)
        {
            pwinnings.setText("You've won: $");
        }
        color.add(pwinnings);
        color.add(winningsField);
        color.setLayout(new FlowLayout());
        add(color, BorderLayout.SOUTH);
        color.setVisible(false);

    }

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == play)
            {
                color.setVisible(true);

            }
            if (e.getSource() == exit)
            {
                dispose();
            }
            if (e.getSource() == replay)
            {

            }
        }

    }

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub

        JFrame frame = new HotAndColdGUI(1366, 768);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}   

1 个答案:

答案 0 :(得分:1)

你一直在赢,因为你从未真正“玩”

制作GUI时,首先将值放入moneyEarned,然后转到开关 但是在构建GUI时,两个复选框都没有被选中。

所以hot.isSelected和cold.isSelected都是false。 这意味着您的moneyEarned未设置为0并且您总是赢。