user2932121
user2932121

Reputation:

How would I stop the dice rerolling during the loop?

How would I get the dice to not reroll everytime the menu is appeared:

When I press the menu 1 option, and the loops happens how could I get the value to stay the same until I CHOSE the dice to reroll:

import java.io.InputStream;
import java.util.Scanner;

class RecordDice {
    public static void main(String[] args) {

        int dSides, Sides, Choice;
        int max = 0, min = 0;
        Scanner s = new Scanner(System. in );
        Scanner c = new Scanner(System. in );

        boolean keepgoing = true;

        System.out.println("How many sides should the dice have?");
        Sides = s.nextInt();
        if (Sides == 4 || Sides == 6 || Sides == 12 || Sides == 20 || Sides == 100) {

            while (keepgoing) {

                System.out.println("Please make a choice:\n" +
                    "1 - reroll the dice\n" +
                    "2 - get the value\n" +
                    "3 - show the maximum\n" +
                    "4 - show the minimum");

                Dice2 d = new Dice2(Sides);
                Choice = c.nextInt();
                int Value = d.getValue();

                if (Value > max) {
                    max = Value;
                }
                if (Value < min) {
                    min = Value;
                }


                if (min > max) {
                    max = min;
                }

                switch (Choice) {
                    case 1:

                        d.reroll();
                        break;
                    case 2:
                        System.out.println("The current value is " + Value);
                        break;
                    case 3:
                        System.out.println("The maximum is " + max);
                        break;
                    case 4:
                        System.out.println("The minimun is " + min);
                        break;
                }
            }
        }
    }
}

Upvotes: 1

Views: 318

Answers (2)

Govan
Govan

Reputation: 7781

Change your code like this:

System.out.println("How many sides should the dice have?");
Sides = s.nextInt();

if(Sides == 4 || Sides == 6 || Sides == 12  || Sides == 20 ||  Sides == 100){
   Dice2 d = new Dice2(Sides);

    while (keepgoing) {
        System.out.println("Please make a choice:\n" +
            "1 - reroll the dice\n" +
            "2 - get the value\n" +
            "3 - show the maximum\n" +
            "4 - show the minimum");

           Choice = c.nextInt();
           int Value = d.getValue();

           if(Value > max){
              max = Value;
           }
           if(Value < min){
              min = Value;
           }

Upvotes: 0

Alex Santos
Alex Santos

Reputation: 2950


it doesn't seem like your dice is rerolling. Although I can't see how the class Dice2 looks like, I suppose that your problem is that you're creating a new Dice2 in every iteration of the loop. So you're losing the reference to the previous roll.
Place the Dice2 d = new Dice2(Sides) outside of the while loop. It may look something like this:

Dice2 d = new Dice2(Sides);

while (keepgoing) {

    System.out.println("Please make a choice:\n" +
    "1 - reroll the dice\n" +
    "2 - get the value\n" +
    "3 - show the maximum\n" +
    "4 - show the minimum");

    Choice = c.nextInt();
    int Value = d.getValue();

    if(Value > max){
        max = Value;
    }
    ...

Hope that helps with your problem! (Note that this solution is assuming that you're storing the Dice's roll value within the Dice2 object)

Upvotes: 1

Related Questions