+ 2
While loop java
It's very simple but still I can't get it. Who has a clue? http://programmingbydoing.com/a/dice-doubles.html
5 Answers
+ 4
William gotcha :) im glad you got it working, feel free to ask if you have any other questions!
+ 3
William well assuming yoy have a method to roll each die and ascertain the variable
dieVal1= 0;
dieVal2=1;
while(dieVal1 != dieVal2){
dieVal1 = die1.roll();
dieVal2 = die2.roll();
System.out.println(""+dieVal1 + dieVal2);
}
+ 3
Robert Atkins, thanks, man! But I think I figured out what was the mistake in my code, here is the right one:
import java.util.Random;
public class Dice {
public static void main(String[]args){
Random r = new Random();
int one;
int two;
System.out.println("HERE COMES THE DICE!!!");
System.out.println();
one = r.nextInt(6)+1;
System.out.println("Roll #1: "+one);
two = r.nextInt(6)+1;
System.out.println("Roll #2: "+two);
System.out.println();
System.out.println();
while(one!=two) {
System.out.println("HERE COMES THE DICE!!!");
System.out.println();
one = r.nextInt(6)+1;
System.out.println("Roll #1: "+one);
two = r.nextInt(6)+1;
System.out.println("Roll #2: "+two);
System.out.println();
System.out.println();
}
}
}
My mistake was these two lines were in the wrong order:
one = r.nextInt(6)+1;
System.out.println("Roll #1: "+one);
two = r.nextInt(6)+1;
System.out.println("Roll #2: "+two);
My wrong code went like:
System.out.println("Roll #1: "+one);
one = r.nextInt(6)+1;
System.out.println("Roll #2: "+two);
two = r.nextInt(6)+1;
+ 2
could you elaborate?
+ 2
It's a dice game, there are two dice that are played simultaneously, called Roll #1 and Roll #2. The task is to use a while loop that will stop the game as soon as both Rolls get the same number.