-2

I am currently working on a project which involves a dice game where you click "roll" and the dice generates the number and lists it on a JTextArea and adds to the total number below.

When the user gets a 3, the total number turns to 0.

My problem is that when pressing "roll" the total number doesn't add the rolled number each time instead it shows the current rolled number.

JButton roll=new JButton("Roll");
roll.setBounds(900,750,75,30); 
roll.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        String command = e.getActionCommand();
        if (command.equals ("Roll")){
               
            int player1score = 0;
            int player2score = 0;
            int total = 0;
            int total2 = 0;

            Integer dice=(int)(Math.random()*6+1);

            if (dice==1 || dice==2 || dice==4 || dice==5 || dice==6) { 

                area1.append(String.valueOf(dice + "\n"));

            } else if(dice==3) {  

                dice = 0;
                total = 0;
                area1.setText(String.valueOf(total + "\n"));
                totalbox1.setText(String.valueOf(total + "\n"));

            }

        total = total + dice;
        totalbox1.setText(String.valueOf(total2));

        }
    }
});
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 3
    If I understands correctly: ```total``` is a variable which should accumulate rolls. If yes, this it should be out of scope of lambda. In your code ```total``` is local variable and it's initialized to 0 each time. – The Tosters Jan 10 '22 at 19:26
  • 1
    Please see [mcve] and enhance your question accordingly. Meaning: show us the relevant parts of your class. I am guessing that you have issues with *shadowing* your `total` information. It looks like you have a **field** in your class with that name, and also a **local** variable in that method. It is really not clear how things are supposed to work here, – GhostCat Jan 10 '22 at 19:27
  • 2
    You need to take the time to learn the difference between local and instance fields/variables, for [example](https://stackoverflow.com/questions/20671008/what-is-the-difference-between-a-local-variable-an-instance-field-an-input-par) and [example](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – MadProgrammer Jan 10 '22 at 20:35

1 Answers1

1

You're resetting total to 0 at the beginning of the if statement. So every time you click roll, the line int total = 0; is being triggered, and at the end you add 'dice' to 'total' , which will always return dice.

You should instead initialize the variables outside of the if statement.

Alex
  • 63
  • 8
  • There's no loop in the question – OneCricketeer Jan 10 '22 at 19:40
  • Edited thanks, point stands though, when he clicks roll, the total is reset to 0 – Alex Jan 10 '22 at 19:42
  • Right. This answers the problem, but what is the fix? – OneCricketeer Jan 10 '22 at 19:44
  • A dice game with a JText Panel sounded like homework / school project to me, thought we werent allowed to give answers to them, he should initialize the variable outside of the statement. If Im wrong about those rules I'll update my answer above, thanks. – Alex Jan 10 '22 at 19:54
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '22 at 23:26
  • @Alex - "thought we weren't allowed to give answers to them". There are no rules as such, but there are different opinions on the best way to answer homework questions. See [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822). – Stephen C Jan 11 '22 at 00:44
  • In this example, I would be disinclined to spell out the fix for the OP. It is better if you give them the clues to work it out for themselves. It is more important that they *understand* the problem / solution than simply "getting the right answer". If they don't understand ... it will be difficult for them was the programing tasks get longer and harder. – Stephen C Jan 11 '22 at 00:50