1

I am new to Java and writing code. I want to program a simple board game. But if I roll the dice, I get the same number every roll. What did I do wrong?

I have a class to create a random between 1 and 6:

public class Dice {
 public int throwDice() {
 Random random = new Random();
 int dice = random.nextInt(6) + 1;
 return dice;
 }
}

And the main class:

public class BoardGame{
 public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 String r;
 Dice dice = new Dice();
 int roll = dice.throwDice();
 int position = 0;
 int finish = 40;
 while (finish >= position) {
 do {
 System.out.print("Roll the dice (r): ");
 r = input.nextLine();
 } while (!r.toLowerCase().equals("r"));
 if (r.toLowerCase().equals("r")) {
 System.out.println("You have rolled " + roll + ".");
 position += roll;
 System.out.println("You are now on square " + position + ".");
 }
 }
 System.out.println("You won!");
 input.close();
 }
}

Thank you!

asked Mar 11, 2019 at 13:51
3
  • 5
    Because you do not give a new value to roll variable inside the loop. Commented Mar 11, 2019 at 13:52
  • you only roll once. or do you mean between different times running this code? Commented Mar 11, 2019 at 13:53
  • Thanks, George Z. I now see what I did wrong. Commented Mar 11, 2019 at 13:55

4 Answers 4

2

The code that does get a random result:

int roll = dice.throwDice();

runs exactly once. You call the throw method once. You store the result, not a "pointer" to a function that would get invoked repeatedly whenever roll gets used somewhere.

So you should put that line:

roll = dice.throwDice();
System.out.println("You have rolled " + roll + ".");

right in front of the place where you expect another roll of the dice!

achAmháin
4,2764 gold badges20 silver badges42 bronze badges
answered Mar 11, 2019 at 13:54
Sign up to request clarification or add additional context in comments.

Comments

1
public class Dice {
 private Random random;
 public Dice(){
 random = new Random();
 }
 public int throwDice(){
 int dice = random.nextInt(6) + 1;
 return dice;
 }
}

This will work because your Dice now has one random instance which is generating new random integers everytime throwDice() is called

answered Mar 11, 2019 at 14:06

Comments

0

After you add int roll = dice.throwDice(); to your loop you may find that you get the same sequence of roles each time. If you do not want that you will have to set the random seed.

See this question: Java random numbers using a seed

answered Mar 11, 2019 at 14:05

Comments

0

Change your Dice class to this.

public class Dice {
 private Random random;
 public Dice(){
 random = new Random();
 }
 public int throwDice(){
 int dice = random.nextInt(6) + 1;
 return dice;
 }
}

And update roll = dice.throwDice(); inside the loop. This will work because your Dice now has one random instance which is generating new random integers everytime throwDice() is called.

answered Mar 11, 2019 at 14:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.