Reputation: 153
I'm working on a program for my classes lab and I seem to be using nextInt wrong and I was wondering if someone could help. I couldn't find a post that had an answer. The program is designed to experiment with loops in a rolling dice simulation. It it supposed to roll two dice, determine the result, and then make a tally if it's snake eyes or doubles or whatever it may be.
So I get the following error:
DiceSimulation.java:33: error: cannot find symbol
die1Value = nextInt();
^
symbol: method nextInt()
location: class DiceSimulation
DiceSimulation.java:34: error: cannot find symbol
die2Value = nextInt();
^
symbol: method nextInt()
location: class DiceSimulation
2 errors
This is the code I have, I'm not entirely sure how I've used nextInt wrong.
while (count < NUMBER)
{
die1Value = nextInt();
die2Value = nextInt();
if (die1Value == die2Value)
{
if (die1Value == 1)
{
snakeEyes += 1;
}
else if (die1Value == 2)
{
twos += 1;
}
else if (die1Value == 3)
{
threes += 1;
}
else if (die1Value == 4)
{
fours += 1;
}
else if (die1Value == 5)
{
fives += 1;
}
else if (die1Value == 6)
{
sixes += 1;
}
}
count += 1;
}
Upvotes: 0
Views: 190
Reputation: 106480
Random#nextInt()
is neither a static method, nor built-in - you have to have an instance of the Random
class to use it.
Here's an example:
Random dice = new Random();
int die1Value = dice.nextInt(6) + 1;
int die2Value = dice.nextInt(6) + 1;
The addition of 1 there is to offset the fact that a random value with a range bound generates values between [0, n).
Upvotes: 4