0

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?

3
  • 1
    If you want to generate a random int within a range. This is an inefficient method. Commented May 29, 2021 at 4:43
  • Also, you need a for loop to iterate over each element and compare. Commented May 29, 2021 at 4:48
  • I used that method but that wasn't effective :< Commented May 29, 2021 at 4:50

1 Answer 1

1

You need a for loop and compare all the values in the array with the user input. Right now you are just comparing the element at count which is always going to be the last element.

while (notReg) {
   System.out.print("\nEnter number: ");
   int userGuess = scan.nextInt();
   for (int element : sukicode) {
      if (element == userGuess) {
         notReg = false;
         System.out.println("Hi");
         break; // Quits for loop early since there is no need to compare rest of elements
      }
   }
   // If none of the elements matched the input then this will print
   if (notReg) {
      System.out.println("You're not in yet!");
   }
}

To generate random numbers above 9000 you can do

int randomNumber = rand.nextInt(Integer.MAX_VALUE) + 9000;  // The lower bound will be 9000
System.out.println(randomNumber);
sukicode[count] = randomNumber;
count++;

Note that I shifted the count++ after the insertion in array. This was done so the first index of the array that is 0 is utilized. Otherwise it stores the first value at index 1.

Output:

194403844
[1]Display array?
[2]Log in
[3]Generate random number
Select: 2

Enter number: 194403844
Hi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.