-3

The code must throw a dice repeatedly until it is six. Currently I am using the following code and a (set) represents a side of the dice.

    // Throwing the dice
double r = (int) (6 * Math.random()) +1;        
  //Printing the result
  if (r==1)System.out.print(set1);
  else if (r==2)System.out.print(set2);
  else if (r==3)System.out.print(set3);  
  else if (r==4)System.out.print(set4);  
  else if (r==5)System.out.print(set5);  
  else if (r==6)System.out.print(set6);  
   do    {    } while(r != 6);  

My question is how I can change the code so it keeps on throwing the dice untill it throws 6.

stevv
  • 51
  • 7

3 Answers3

1

What errors did you get with your code?

It seems that's it's almost correct: as @StephenC said, just put the code inside the loop, like so (I also implemented switch() to make this cleaner):

double r;
// code goes INSIDE the `do ... while` loop
// each iteration is a new throw of the dice
do {
    // assign random value to int
    r = (int) (6 * Math.random()) +1;        
    // Printing the result
    switch(r) {
        case 1:
            System.out.print(set1);
            break;
        case 2:
            System.out.print(set2);
            break;
        case 3:
            System.out.print(set3);
            break;
        case 4:
            System.out.print(set4);
            break;
        case 5:
            System.out.print(set5);
            break;
        case 6:
            System.out.print(set6);
            break;
    } 
} while(r != 6);  
Jonathan Lam
  • 16,198
  • 15
  • 63
  • 88
1

Put the code into the loop. Your variable r (which should be an int, since it's always a whole number) should be initialized within the loop. You might also consider creating an array for your printable results (assuming that it is of type String..). It will make your code look much nicer:

String[] sets = new String[6];
//initialize array with values..
int r;
do{
    r = (int) (6 * Math.random());
    System.out.print(sets[r]);
} while(r != 5);

Also consider replacing your generation of r with this code:

//Create an Object of type Random. This will be used to create our integers.
//Make sure not to create it inside the loop!
Random ran = new Random();
int r = ran.nextInt(6)+1;

This will create a random integer in range [0,5], which is more efficient than creating a random double, multiplying and casting it.

If you ever find yourself in a similar situation but you can't use an array (because you actually need to run different parts of code, not just access a different object), you should still not use the if-statements like you did. Your variable r will be compared with all values until you get a hit. Rather use a switch case statement. They are better in performance (you have a lookup table, rather than comparing your number r to all possible values)

Random ran = new Random();
int r;
do{
    r = ran.nextInt(6)+1;
    switch(r){
        case 1:
            //do stuff after rolling 1
            break;
        // cases 2-5
        case 6:
            //do stuff after rolling 6
            break;
        default:
            throw new RuntimeException("x < 1 || x > 6")
    }
} while(r != 6);
-1

The first problem I see is you're casting Math.random() to int and storing it as a double.

double r = (int) (6 * Math.random()) +1;

This should look like this:

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

This results in 2 instead of 2.0. Doesn't really make a difference, but if you cast a double value to int you should store it as such. Otherwise, why cast it at all? You may also like to know about the Random class. I think it looks a little cleaner:

int number = new Random().nextInt(6)+1;

Some would argue that Random is more efficient than Math.random(). See here.

Additionally, instead of if/else statements for this program I would recommend a switch statement like this:

switch (number) {
    case 1:
        System.out.println("Case 1");
        break;
    case 2:
        System.out.println("Case 2");
        break;
    // so on, so forth
}

To sum it all up, try something like this:

import java.util.Random;

public class NewClass {
    public static void main(String[] args) {
        int number = 0;
        while (number != 6) {
            number = new Random().nextInt(6) + 1;
            switch (number) {
                case 1:
                    System.out.println("Case 1");
                    break;
                case 2:
                    System.out.println("Case 2");
                    break;
                case 3:
                    System.out.println("Case 3");
                    break;
                case 4:
                    System.out.println("Case 4");
                    break;
                case 5:
                    System.out.println("Case 5");
                    break;
                case 6:
                    System.out.println("yay!");
                    break;
                default:
                    System.out.println("default");
                    break;
            }
        }
    }
}
  • Math.random() just creates a Random object and uses it. It is therefore not by itself more efficient. The gain in efficiency comes from the fact that you can create the object ones and call it several times (which you unfortunately didn't do..). Also: creating an int instead of creating a double, multiplying and casting it is a lot more efficient. That method is unfortunately not provided by Math – Neuron - Freedom for Ukraine Oct 24 '15 at 00:14