简体   繁体   中英

Coin flip simulation: Counting heads / tails

I am trying to simulate a coin flipper using Java and window builder. I have a button called "Flip" when that button is pressed the coin image changes depending on which number the random generator I created generates.

I now am trying to figure out a way to display the number of times the coin lands a heads or a tails in their respective JTextFields . I was thinking of using a counter, but I am struggling with how to update that into the text field, so far it only puts in that I have flipped each coin once.

I am very new to programming so any advice or guidance is much appreciated.

// this button flips the coin
btnFlip = new JButton("Flip");
btnFlip.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        int headCounter = 0;
        int tails = 0; 
        // this implements the random flip of the coin when the checkbox Run Multiple
        // flips is unchecked
        if (chckbxNewCheckBox.isSelected() == false) {

            Random r = new Random();
            int flipper = r.nextInt(2);
            if (flipper == 1) { 
                lblImages.setIcon(new ImageIcon(FinalPrep.class.getResource("/finalPrep/heads.png")));
                textFieldHeads.setText(String.valueOf(headCounter));

            } else {
                lblImages.setIcon(new ImageIcon(FinalPrep.class.getResource("/finalPrep/tails.png")));
                textFieldTails.setText(String.valueOf(tails));
            }
        }
    }
});
btnFlip.setFont(new Font("Tahoma", Font.PLAIN, 15));
panel_1.add(btnFlip);

You have to create something like 2 layers. One that contains the window itself along with the title (think of it like a receptor) then you have to create another "layer" that contains the buttons and the textfields. Something like this:

import javax.swing.*;

public class MyFrame extends JFrame {

    private JPanel panel;
    private JTextField textField;
    private JButton button;

    public MyFrame(){

        panel = new JPanel(); //Step 1. Creation of a receptor

        tfCount = new JTextField(10); //Step 2.
        button = new JButton("Press Me"); //Creation of buttons & textfields.

        panel.add(tfCount); //Step 3.
        panel.add(button); //Add those graphics to the receptor  

        this.setContentPane(panel); //Step 4 Adjust the receptor to the object

        this.setVisible(true);
        this.setSize(400, 400);
        this.setTitle("My 1st GUI!");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
} 

(Sorry for the random example but you did not post any sample of code so I am trying to explain as best as I can)

Then you should put a "flag" depending of the number that was generated for example:

    boolean flag = true; //Flag

    int counter = 0; //The counter for the heads

    int x; //The number you received from the generator

    int counter_b; //The counter for the tails

    if(x%2 == 0){ //If the number is even
        flag = false; // You set down the flag
        counter++; //And counter raises its value
    }
    else
        counter_b++;


    tfCount.setEditable(true); //This makes your textfield editable
    tfCount.setText(String.valueOf(counter)); //And this prints the counter's value

Keep in mind that the counters for the tails and the heads are just examples for you to understand how to use "flags". I hope that I was able to help you because I am new too!! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM