Search code examples
javauser-interfacenetbeansdice

Challenges with conditions


Hello I am new to Java and this is what I have to do: I have to make a point game and I am not posting the entire thing as it is irrelevant. There are two diсes, one with 6 sides and the other with 9 sides. This is what I have a problem with:

If you roll double 6’s at least 5 times in a single round, the following will occur:

  • The dice are rolled a random number of times (between 10 and 30 times)

  • The cumulative product of all those dice rolls are added to the bank

I don't really understand the wording here and I have done the whole program except for this part. This is what I have done:

if (ninedice == 6 && sixdice == 6 >= 5){
    ninedice = (int) Math.ceil(Math.random () * (30-10)+ 10);
    sixdice = (int) Math.ceil(Math.random () * (30-10)+10);
    accountpoints = ninedice+sixdice;
}

This is obviously not correct and I am not getting the desired result as it just does not roll the die a random number of times and does not add points in the account either. ANY HELP IS GREATLY APPRECIATED AND THANK YOU IN ADVANCE!


Solution

  • So you're on the right track but not quite there yet. The if statement condition should just be if (ninedice == 6 && sixdice == 6). No need for the >= 5 part. This if statement signifies indicates whether or not the first roll for each die reuslts in a 6. However, the if-statement body has some logic issues. This body of code,

    ninedice = (int) Math.ceil(Math.random () * (30-10)+ 10);
    sixdice = (int) Math.ceil(Math.random () * (30-10)+10);
    

    generates the number of times the two die are going to be rolled again. However, ninedice and sixdice represent the first rolls so don't overwrite them. Replace it with,

    int ninedicerolls = (int) Math.ceil(Math.random () * (30-10)+ 10);
    int sixdicerolls = (int) Math.ceil(Math.random () * (30-10)+10);
    

    Now keep a product variable to keep track of each roll for each die,

    int ninediceproduct = ninedice;
    int sixdiceproduct = sixdice;
    while(ninediceproduct > 0){
        ninediceproduct *= (int) Math.ceil(Math.random () * (9-1)+ 1);
        ninediceproduct--;
    }
    while(sixdiceproduct > 0){
        sixdiceproduct *= (int) Math.ceil(Math.random () * (6-1)+ 1);
        sixdiceproduct--;
    }
    

    Then, multiply the products together,

    int cumulative = sixdiceproduct * ninediceproduct;