簡體   English   中英

if 語句和 switch 問題

[英]If-statement and switch problems

我正在嘗試制作一個游戲,其中用戶將使用提供的微調器輸入他們的賭注,然后選擇冷或熱單選按鈕之一。 這些單選按鈕基於冷色(紫綠色藍色)或熱色(黃色、紅色、橙色)的色輪顏色。 這些顏色作為圖像保存在我的數組中,顏色是隨機選擇的,並且會出現在一個不可見的面板中,一旦您點擊播放按鈕,該面板就會變得可見。 您的賭注被接受,並根據金額增加一倍、三倍或四倍。 如果顏色是白色或黑色,房子會贏而用戶不會收到錢,如果他們選擇冷色而顏色是熱色,則同樣如此,反之亦然。 我遇到的問題是在玩游戲時,即使顏色是冷的,我選擇了熱,但我仍然獲勝,或者如果顏色是熱的,我選擇了冷,我看不到錯誤在哪里,需要一些幫助。

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);

    }

}   

你一直在贏,因為你從來沒有真正“玩”過

當你制作你的 GUI 時,你首先在 moneyEarned 中輸入一個值,然后你去你的開關,但是在你的 GUI 構建時,你的復選框都沒有被選中。

所以 hot.isSelected 和cold.isSelected 都是假的。 這意味着您的 moneyEarned 未設置為 0,您總是贏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM