0

A link to the assignment: https://i.sstatic.net/utlLG.png

I'm having a bit of trouble discerning how to take a series of numbers and apply them to an array without a loop. Not only that, but I'm having a bit of trouble comparing them. What I have written so far is:

import java.util.Scanner;

public class Lottery {

public static void main(String[] args) {
 int userInputs[] = new int[5];
 int lotteryNumbers [] = new int[5];
 int matchedNumbers =0;
 char repeatLottery = '\0';


    Scanner in = new Scanner (System.in);

    do{
        System.out.println("Enter your 5 single-digit lottery numbers.\n (Use the spacebar to separate digits): ");
            for(int i = 0; i <5; i++ )
            userInputs[i] = in.nextInt();

            System.out.println("Your inputs: ");
            printArray(userInputs);

        System.out.println("\nLottery Numbers: ");
        readIn(lotteryNumbers);
        for(int i=0; i<5; i++) {
            System.out.print(lotteryNumbers[i] + " ");
        }

        matchedNumbers = compareArr(userInputs, lotteryNumbers);

        System.out.println("\n\nYou matched " + matchedNumbers + " numbers");



        System.out.println("\nDo you wish to play again?(Enter Y or N): ");
         repeatLottery = in.next().charAt(0);
    }
    while (repeatLottery == 'Y' || repeatLottery == 'y');

}
public static void printArray(int arr[]){

    int n = arr.length;

    for (int i = 0; i < n; i++) {
        System.out.print(arr[i] + " ");
    }
}

public static void readIn(int[] List) {
    for(int j=0; j<List.length; j++) {
        List[j] = (int) (Math.random()*10);
    }
}

public static int compareArr (int[] list1, int[] list2) {
    int same = 0;
    for (int i = 0; i <= list1.length-1; i++) {
        for(int j = 0; j <= list2.length-1; j++) {
            if (list1[i] == list2[j]) {
                same++;


            }

        }
    }
    return same;
}

}

As you'll notice, I commented out the input line because I'm not quite sure how to handle it. If I have them in an array, I should be able to compare them fairly easily I think. This is our first assignment handling arrays, and I think it seems a bit in-depth for only having one class-period on it; So, please forgive my ignorance. :P

Edit:

I added a new method at the end to compare the digits, but the problem is it compares them in-general and not from position to position. That seems to be the major issue now.

2
  • The commented out portion should already be the correct way to write it? Then loop through and compare the two arrays. Commented Mar 28, 2013 at 0:35
  • So you're not allowed to use a for loop to set the values of the array? Is that the case? It's unclear what your question is. Commented Mar 28, 2013 at 0:36

1 Answer 1

1

your question isn't 100% clear but i will try my best. 1- i don't see any problems with reading input from user

int[] userInput = new int[5]; // maybe here you had a mistake
int[] lotterryArray = new int[5]; // and here you were declaring your arrays in a wrong way
Scanner scanner = new Scanner(system.in);
for ( int i = 0 ; i < 5 ; i++)
{
 userInput[i] = scanner.nextInt();
} // this will populate your array try to print it to make sure

Edit : important in the link you shared about the assignment the compare need to check the value and location so if there are two 5 one in input one in loterry array they need to be in the same location check the assignment again

// to compare
int result = 0 ; // this will be the number of matched digits
for ( int i = 0 ; i < 5 ; i++)
{
   if ( userInput[i] == loterryArray[i] )
       result++
}
// in this comparsion if the digits are equale in value and location result will be incremented  
Sign up to request clarification or add additional context in comments.

2 Comments

The comparison worked. I created an int method that returned the result. Thank you!
Edit : important,,,,,, in the link you shared about the assignment the compare need to check the value and location so if there are two 5 one in input one in loterry array they need to be in the same location check the assignment again

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.