-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
  • 1
    What is your question? – Jonathan Lam Oct 23 '15 at 23:13
  • Cross out java and ask: algorithm to through a dice until it delivers six. There is no java in this question, but then such an algorithm is a one-liner: `while (some_random_from_1_to_6() != 6);`. How more trivial can it get? – Oleg Sklyar Oct 23 '15 at 23:15
  • If you want feedback, please make sure that you express how people can help you. Refer to this page to learn how to ask proper questions: http://stackoverflow.com/help/how-to-ask – Neuron Oct 23 '15 at 23:15
  • Kind of off topic, but a `switch()` statement would make this so much cleaner – Jonathan Lam Oct 23 '15 at 23:15
  • Well, if you're always looping until it's a 6, without doing anything else, why not just don't do that? I'm not sure what you want to accomplish. – Erik Pragt Oct 23 '15 at 23:16
  • Sorry, new on stack overflow. My question is how I can change the code so it keeps on throwing the dice untill it throws 6. – stevv Oct 23 '15 at 23:18
  • 2
    Hint: put the code that throws the dice, prints it ... inside the loop. – Stephen C Oct 23 '15 at 23:20
  • @ErikPragt yep, that is what I like to realize but as a java newbie I can't figure it out. – stevv Oct 23 '15 at 23:21
  • @StephenC didn't realize you commented, just wrote an answer about that, didn't mean to copy you... Check it out stevv. – Jonathan Lam Oct 23 '15 at 23:22
  • What are the `set`s again? I mean data type? – ChiefTwoPencils Oct 23 '15 at 23:30
  • 1
    One thing: you want to read about ARRAYS. It is rather a complete waste of energy to compute a number, and then to a switch to select one of 6 variables. – GhostCat Oct 23 '15 at 23:30

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,831
  • 17
  • 68
  • 94
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);
Neuron
  • 5,141
  • 5
  • 38
  • 59
  • hey, stop copying! =) – Jonathan Lam Oct 23 '15 at 23:32
  • Shouldn't that `Throw` be lowercase? And wouldn't the scope of `r` be limited to inside the loop (will throw an error in the last line, `r != 6`)? I haven't tried it but I think this might happen – Jonathan Lam Oct 23 '15 at 23:33
  • @jlam55555 I didn't.. I was just slower :( also: yes, that throw should be lower case and the r should be initialized earlier, thanks ;) – Neuron Oct 23 '15 at 23:36
  • @ChiefTwoPencils well, but he needs to be tought about switch case statements! I moved it to the bottom of my answer ;) – Neuron Oct 23 '15 at 23:37
  • @Neuron actually I give you credit back: I like the array solution to remove the `switch` or `if` ladder altogether – Jonathan Lam Oct 23 '15 at 23:38
  • @ChiefTwoPencils Yes, you are right. I addressed both issues with the last edits. – Neuron Oct 23 '15 at 23:43
  • 1
    that's what happens when you edit your code a thousand times :P – Neuron Oct 24 '15 at 00:04
-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;
            }
        }
    }
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
  • 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 Oct 24 '15 at 00:14