While loop java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

17th Aug 2018, 1:40 PM
William
William - avatar
5 Answers
+ 4
William gotcha :) im glad you got it working, feel free to ask if you have any other questions!
18th Aug 2018, 7:52 AM
Robert Atkins
Robert Atkins - avatar
+ 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); }
17th Aug 2018, 11:23 PM
Robert Atkins
Robert Atkins - avatar
+ 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;
18th Aug 2018, 2:01 AM
William
William - avatar
+ 2
could you elaborate?
17th Aug 2018, 1:49 PM
Robert Atkins
Robert Atkins - avatar
+ 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.
17th Aug 2018, 10:27 PM
William
William - avatar