Java ".class expected"
import java.util.Random;
import java.util.Scanner;
public class Lottery
{
private int[] lotteryNumbers = new int[5];
private int counter;
private int[] userNumbers = new int[5];
private Scanner keyboard = new Scanner(System.in);
public Lottery()
{
for(counter = 0; counter < 5; counter++)
{
lotteryNumbers[counter] = nextInt(int 10);
}
}
There is more code after the code, but there are no errors there, so I'm not going to include it. Anyway, the line that says "lotteryNumbers [counter] = nextInt (int 10);" get the error ".class expected".
+3
source to share
4 answers
Java is an object oriented language. Which object are you referring to nextInt(10)
? I do not see him. The compiler will assume this implicitly. Does your Lottery
instance use it Random
somewhere? I don't see that.
I think you want something like this:
private Random random = new Random(System.currentTimeMillis());
Then your loop should do this:
lotteryNumbers[counter] = this.random.nextInt(10);
I have other problems with what you are doing:
- Unnecessary "magic" numbers all over the world. This allows this class to be much more flexible than what you have.
- Mixing input into classes like this is a bad idea. Do an abstraction so you can pass in values ββand leave them when you get them. Think of "single responsibility".
- I don't understand why
Lottery
a private data member is needed for custom numbers. However, I can see where it might have a method that will take user numbers and determine if they won or not. In my opinion, you created a bad abstraction.
This may take some time.
+2
source to share