Hi guys,

I need to create the following program:

  1. Declare 2 5-element integer arrays.
    (a) One should be for the winning lottery number (hard code this as 12-35-34-2-5).
    (b) The other should be for the user's lottery number input.
  2. Declare an integer to count the number of matching lottery numbers.
  3. Prompt the user to enter their 5 digit lottery number using a for loop and put their numbers in
    the user's lottery number array.
  4. Compare the elements of the two arrays using a for loop and increment the counter variable
    declared above when there is a match.
  5. Using the value held in the counter variable, display how much the user has won.
    (a) 0 matching numbers: $0
    (b) 1 matching number: $1
    (c) 2 matching numbers: $50
    (d) 3 matching numbers: $1,000
    (e) 4 matching numbers: $50,000
    (f) 5 matching numbers: $90,000,000

I have the code below so far but am consufed and lost as can be, how do i compare the arrays?

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;


public class Lab6Array {

    public static void main(String[]args){

        final int [] HardCoded = {12, 35, 34, 2, 5};
        int UserInput [] = new int[5];

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter 5 Numbers. ");

        System.out.print("Numbers [0]: ");
        UserInput [0] = input.nextInt();

        System.out.print("Numbers [1]: ");
        UserInput [1] = input.nextInt();

        System.out.print("Numbers [2]: ");
        UserInput [2] = input.nextInt();

        System.out.print("Numbers [3]: ");
        UserInput [3] = input.nextInt();

        System.out.print("Numbers [4]: ");
        UserInput [4] = input.nextInt();

        System.out.println("Lottery Numbers are: " + Arrays.toString(HardCoded));

        for(int i = 0; i < HardCoded.length; i++){
            if(HardCoded[i] == UserInput){


            }
        }


            }

    }

Hi Adnan
I suspect the reason why you have no replies yet is that the problem specification seems odd. Step 4 says "compare the elements using a loop", but you can only s
do it with a single loop if you need to match the numbers and their positions, which is not how lotteries actually work.
Anyway, if that is really what your teacher wants then you are almost there. Go back and complete step 2, then you will have a counter that you can increment when you find matching numbers in matching array elements. Line 39 you need to look at again - exactly what are you comparing there?

ps. Line 10, "finally" doesn't do what you seem to think - it means the variable HardCoded can never be changed to refer to another array. It doesn't stop you changing the data in that array.
pps: Java coding convention is that variable names a should begin with a lower case letter - it's not essential, but it helps other people when they read your code.

This article has been dead for over six months. Start a new discussion instead.