I made a function that generates random numbers, and I want all of those generated random numbers to be placed inside an array. I made a log in thing, and I want to check if the inputted number match at least one element in the array with the generated numbers.
Now my problem is, whenever I input some number, the program checks only the last element of my array. Here is my code:
int sukicode[] = new int[100];
int count = 0;
boolean again = true, notReg = true;
Scanner scan = new Scanner(System.in);
Random rand = new Random();
while (again) {
int x;
for (x = 1000; x < 9000; ) {
x = rand.nextInt();
}
count++;
System.out.println(x);
sukicode[count] = x;
System.out.println("[1]Display array?\n[2]Log in\n[3]Generate random number");
System.out.print("Select: ");
int ch = scan.nextInt();
if (ch == 1) {
again = false;
for (int i = 1; i <= count; i++) {
System.out.println(sukicode[i]);
}
} else if (ch == 2) {
again = false;
while (notReg) {
System.out.print("\nEnter number: ");
int a = scan.nextInt();
if (a == sukicode[count]) {
notReg = false;
System.out.println("Hi");
} else {
notReg = true;
System.out.println("You're not in yet!");
}
}
} else if (ch == 3) {
again = true;
}
}
Here is the problem in my output:
1918509534
[1]Display array?
[2]Log in
[3]Generate random number
Select: 3
2043799299
[1]Display array?
[2]Log in
[3]Generate random number
Select: 3
279697469
[1]Display array?
[2]Log in
[3]Generate random number
Select: 3
622842603
[1]Display array?
[2]Log in
[3]Generate random number
Select: 2
Enter number: 279697469
You're not in yet!
The number couldn't accept by the program although it's in my array. What am I supposed to do?