0

You will take in a 5 digit lottery number and compare it to a winning lottery number.

If the input lottery number has a certain numbers correct you will tell the user how much they have won.

You should follow these steps:

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.

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.

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:

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: " + HardCoded);    

}

}

However, the issue is that the hardcoded value will not print and int UserInput [] = new int[5]; has to be 4 but i get an error so i have to put in 5?

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31

2 Answers2

0

the issue is that the hardcoded value will not print

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

Otherwise HardCoded will prints address of it.

int UserInput [] = new int[5]; has to be 4 but i get an error so i have to put in 5?

UserInput is of size 5 so you can enter items from 0 to 4 positions. So totally 5 items. Its correct. Array index start from 0.

You need

int UserInput [] = new int[6];

It will takes items from 0 to 5 positions. So totally 6 items in the array.

Edit:

for(int i = 0; i < UserInput.length; i++)
    {
        for(int j = 0; j < HardCoded.length; j++)
        {
            if(HardCoded[j] == UserInput[i])
            System.out.println(UserInput[i] + " Matched at: " + i);
        }
    }
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
-1

You also forget to clear the line of input each time you print out asking for another number.

Try adding the following code in after each time you grab the user input and put it into the UserInput array.

input.nextLine();
Michael
  • 776
  • 4
  • 19