Question

I'm attempting to make a simple dice program that takes user input for the number of sides and number of dice they want to roll and outputs a roll for each dice. I've included a while loop that allows the user to reroll the same number of dice with the same number of sides without having to reenter the info they'd previously entered. The problem I'm having is the dice won't reroll, when I call the "q" method in the "if" statement of the "while" loop. Advice?:

    import java.util.Random;
    import java.util.Scanner;

    public class Choice_Dice { 
static int t = 0, sides, c=0, d =0;
public static void main(String [] Mack){
    Scanner scan = new Scanner(System.in);
    String y ="y";
    System.out.println("You may roll a dice with any number of sides");
    System.out.println("Enter the number of sides you would like to the dice to have: ");
    sides = scan.nextInt();
    System.out.println("Enter the number of dice you want to roll: ");
    t = scan.nextInt();
    q();
    while(y.equals("y"))
    {
        System.out.println("Would you like to roll again(y or n): ");
        y = scan.next();
        if(y.equals("y")){
            q();
        }
        else
            System.out.println("Thanks");
    }
}
public static void q()
{
    int[] x = new int[t];
    c = 0;
    while(c != t)
    {
        x[c] = roll(sides);
        c++;
    }
    while(d != t)
    {
        System.out.println("You rolled " + x[d]);
        d++;
    }
}
public static int roll(int s)
{
    Random generator = new Random();
    int dice = 0;
    dice = generator.nextInt(4) + 1;
    return dice;
}

}

Était-ce utile?

La solution

It comes down to your use of d. You remembered to reset the value of c, but not d.

public static void q()
{
    // Init variables
    int[] x = new int[t];
    c = 0;
    d = 0;

    while(c != t)
    {
        x[c] = roll(sides);
        c++;
    }
    while(d != t)
    {
        System.out.println("You rolled " + x[d]);
        d++;
    }
}

There's no reason that c and d should be static class variables. Instead, you should just declare them right in your method call, like you do with x:

public static void q()
{
    // Init variables
    int[] x = new int[t];
    int c = 0;
    int d = 0;

    while(c != t)
    {
        x[c] = roll(sides);
        c++;
    }
    while(d != t)
    {
        System.out.println("You rolled " + x[d]);
        d++;
    }
}

Autres conseils

The problem is with

while(d != t)
{
    System.out.println("You rolled " + x[d]);
    d++;
}

You increment d to, say, 4, all the while printing the rolls. When this method ends, your while loop comes into play and you input "y". When the method is called again, d has value of 4, why t also has value 4 so d == t. You need to reset it or use a proper for loop to iterate.

Ugly way would be to do

public static void q()
{
    int[] x = new int[t];
    c = 0;
    while(c != t)
    {
        x[c] = roll(sides);
        c++;
    }
    while(d != t)
    {
        System.out.println("You rolled " + x[d]);
        d++;
    }
    d = 0;
}

The pretty way would be

public static void q()
{
    int[] x = new int[t];

    for (int i = 0; i < x.length ; i++) {
        x[i] = roll(sides);
        System.out.println("You rolled " + x[i]);

    }
}

So now you get rid of the x and c static variables. You can also get rid of t with some simple refactoring.


You aren't using sides by the way.

You try this logic for the case while loop

 while(true)
{
    System.out.println("You may roll a dice with any number of sides");
    System.out.println("Enter the number of sides you would like to the dice to have: ");
    sides = scan.nextInt();
    System.out.println("Enter the number of dice you want to roll: ");
    t = scan.nextInt();
    q();

    System.out.println("Would you like to roll again(y or n): ");
    y = scan.next();
    if(y.equals("n")){

        System.out.println("Thanks");
        break;
    }


}

Also check the logic of the method q() : You need to reset values there

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top