I have to create a program in which a user can input a desired sum, then rolls a two six sided dices until their sum is the desired sum. I know that a) I have to use Scanner since it's an interactive program. b) I have to use indefinite loops to solve the problem including the use of random numbers. c) I don't expect you to give me the answer like I'm just copying and pasting. I just want to be guided on what to do so that my code actually compiles.
Here is my code:
import java.util.*;
public class Game {
public static void main(String[] args) {
System.out.println("Try rolling two six-sided dices");
System.out.println("until their sum is your");
System.out.println("desired sum.");
System.out.println();
Scanner console = new Scanner (System.in);
Random r = new Random();
int sum = 0;
int tries = 0;
While (sum != number) {
int roll1 = rand.nextInt(6) +1;
int roll2 = rand.nextInt(6) +1;
sum = roll1 + roll2;
tries++;
}
}
System.out.println("Desired dice sum: ");
int number = console.nextInt();
*I keep getting these 3 compile errors:
Game.java:21: ';' expected While (sum != number) { ^ Game.java:29: expected
System.out.println("Desired dice sum: "); ^ Game.java:29: illegal start of type
System.out.println("Desired dice sum: "); ^ 3 errors
Edit:
Still getting 3 more compile errors
import java.util.*;
public class Game { public static void main(String[] args) { System.out.println("Try rolling two six-sided dice"); System.out.println("until their sum is your"); System.out.println("desired sum."); System.out.println();
Scanner console = new Scanner (System.in);
Random r = new Random();
System.out.println("Desired dice sum: ");
int number = console.nextInt();
int sum = 0;
int tries = 0;
while (sum != number) {
int roll1 = rand.nextInt(6) +1;
int roll2 = rand.nextInt(6) +1;
sum = roll1 + roll2;
tries++;
}
}
}
@Matt:
This is the output that I should have, sorry for not being clear:
Desired dice sum: 9
4 and 3 = 7
3 and 5 = 8
5 and 6 = 11
5 and 6 = 11
1 and 5 = 6
6 and 3 = 9