Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
Iterating through array lists

I'm creating a lottery game where it checks if the user guessed 2, 3, 4, or 5 numbers out of the random lottery numbers, then prints a message. Lottery numbers are from 1 - 100 printed 10 times and users guess 5 numbers. I have tried for loops and if statements and it doesn't print the right message out. Don't mind the variables as I'm just testing out to see what works. My question is, how do I check if the user guessed 2 , 3 , 4 or 5 numbers correctly? Here is my code.

    // first way to check if user guessed correctly 
    
    int  x,y;
    
    for ( int i = 4; i< numbers.length; i++) {
        x = numbers[i];
        for (int j = 0; j < lottery.length; j++) {
            y = lottery[j];
            
            if (x == y) {
                System.out.println("You guessed 5 numbers");
            }
            for ( i = 3; i< numbers.length; i++ ) {
                x = numbers[i];
                
                if ( x == y) {
                    System.out.println("You guessed 4 numbers");
                }
                for ( i = 2; i < numbers.length; i++) {
                    x = numbers[i];
                    
                    if ( x == y) {
                        System.out.println("You guessed 3 numbers");
                    }
                    for ( i = 1; i < numbers.length; i++) {
                        x = numbers[i];
                        
                        if ( x == y) {
                            System.out.println("You guessed 2 numbers");
                        }
                      }
                    }
                }
  
        }
    }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

public static void main(String[] args) {

        int count = 0;
        int[] lottery = new int[5];
        int[] input = new int[5];

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter 5 number: ");
        for(int i = 0; i < 5; i++) {
            input[i] = scanner.nextInt();
        }

        Random r = new Random();
        for (int i = 0; i < lottery.length; i++) {
            int random = r.nextInt(10); // Upper bound of random generator = 10.
            lottery[i] = random;
            System.out.print(lottery[i]);
        }

        for (int i = 0; i < lottery.length; i++) {
            for (int j = 0; j < input.length; j++) {
                if (lottery[i] == input[j]) {
                    count++;
                }
            }
        }

        if (count == 0) {
            System.out.println("\nToo bad, you have 0 correct guess. Try again!");
        } else if (count == 1) {
            System.out.println("\n1 number guessed correctly! You won $1000!");
        } else if (count == 2) {
            System.out.println("\n2 number guessed correctly! You won $6000!");
        } else {
            // Add condition
        }
    }

This will receive 5 random numbers and store it in lottery array. The program will prompt user for 5 input numbers, store it in input and compare lottery[i] with each numbers in input. If two numbers are equal, increment count. Example run:

Enter 5 number: 
1 2 3 4 5
7 2 5 9 1 
2 number guessed correctly! You won $6000!

Note that if the random number generator creates two or more duplicate numbers, the count will increases as well. It's an edge case, but I think you can Google for it if you want the improvement.

Implementing this using ArrayList is also a lot more simple because ArrayList has the method contains() that will check if it has such elements. We can use a trivial if-else loop for it. I assume OP's homework has a constraint of using Array and therefore not using this method.

over 4 years ago · Santiago Trujillo Report

0

I have implemented a complete and fully working example program. It uses ArrayLists to store the different numbers. With shuffle, the lottery numbers are mixed. After all numbers were shuffled, the first 5 elements are taken from the lottery numbers and declared as the winning numbers. All winning numbers are uniqe.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class LotteryGame {

    public static void main(final String[] args) {
        final int N = 5; // Number of winning lottery numbers.

        ArrayList<Integer> winningNumbers = new ArrayList<Integer>();
        ArrayList<Integer> userNumbers = new ArrayList<Integer>(); // Are also 5 numbers (5 out of 1 to 100 lottery
                                                                    // game).
        ArrayList<Integer> lotteryNumbers = new ArrayList<Integer>();

        // Insert user numbers (user's tips).
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please Enter 5 numbers: ");
        for (int i = 0; i < 5; i++) {
            userNumbers.add(scanner.nextInt());
        }

        System.out.println("\nUser's Tips:\n" + userNumbers);

        // Generating the different numbers.
        for (int i = 1; i < 101; i++) { // Lottery game: Numbers from 1 to 100.
            lotteryNumbers.add(i);
        }

        // System.out.println("\nOriginal Lottery Numbers:\n" + lotteryNumbers);

        // Shuffle the lottery numbers randomly.
        Collections.shuffle(lotteryNumbers);

        // System.out.println("\nShuffled Numbers:\n" + lotteryNumbers);

        // Retrieve the N-th first elements from the ArrayList (all numbers are uniqe).
        for (int i = 0; i < N; i++) {
            winningNumbers.add(lotteryNumbers.get(i));
        }

        System.out.println("\nWinning Numbers are:\n" + winningNumbers);

        // Checking the correct guessed numbers.
        int count = 0;
        for (int userTip : userNumbers) {
            if (winningNumbers.contains(userTip)) {
                count++;
            }
        }

        // Print out fi the user has won a money price or not.
        if (count == 0) {
            System.out.println("\nSorry, no numbers guessed!");
        } else if (count == 1) {
            System.out.println("\nYou guessed ONE number, you win $1,000");
        } else if (count == 2) {
            System.out.println("\nYou guessed TWO numbers, you win $6,000");
        } else if (count == 3) {
            System.out.println("\nYou guessed THREE numbers, you win $10,000");
        } else if (count == 4) {
            System.out.println("\nYou guessed FOUR numbers, you win $20,000");
        } else if (count == 5) {
            System.out.println("\nYou guessed FIVE numbers, you win $30,000");
        }
    }
}

Example output:

Please Enter 5 numbers: 
3 7 33 93 43

User's Tips:
[3, 7, 33, 93, 43]

Winning Numbers are:
[54, 31, 48, 64, 82]

Sorry, no numbers guessed!
over 4 years ago · Santiago Trujillo Report

0

Convert arrays to lists and do the checks

 List<Integer> userNumbers = Arrays.stream(numbers).boxed().collect(Collectors.toList());
 List<Integer> lotteryNumbers = Arrays.stream(lottery).boxed().collect(Collectors.toList());


 int count = 0;
 for(int i=0; i<userNumbers.size();i++){
     if(lotteruNumbers.contains(i)){
          count++
     }
 }

 System.out.println("You've guessed"+ count+ " numbers"); 
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!