Java Beginner: Scanner Console and two 6-sided dice problem
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开发者_Python百科: ';' 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
Why do you have:
System.out.println("Desired dice sum: ");
int number = console.nextInt();
Out side of main? Put it back in.
Also, while is not the same as While change it to while.
In response to your new problems:
int roll1 = rand.nextInt(6) +1;
Should be something like:
int roll1 = r.nextInt(6) + 1;
Because you are making a variable r for your random not a variable rand.
It should be
while
, notWhile
.You're missing a closing brace (
}
) at the end of your code.The (current) last two lines need to be inside of a method.
You have not declared a variable named
number
properly. This is related to problem #3.You first call your random generator
r
, but later try to reference it byrand
.
Try something like this (untested):
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();
System.out.println("Desired dice sum: ");
Scanner console = new Scanner (System.in);
int number = console.nextInt();
Random rand = 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("Found sum: " + sum);
}
System.out.println("Done in " + tries + " tries");
}
}
The last 2 lines:
System.out.println("Desired dice sum: ");
int number = console.nextInt();
are outside of your main. They need to be in main. If you want numbers to be a member of your class, declare it above your main, and initialize it in main, like:
private int number;
public static void main(String args[])
{
...
number = console.nextInt();
...
}
Also, you can't make that System.out.println outside of a method. Put it in main.
精彩评论