Mano Mano - 22 days ago 7
Java Question

Matching Array Numbers By Column

I have been asked to implement a lottery program. The code is as follows..

Scanner keyIn = new Scanner(System.in);
Random randomGenerator = new Random();

int numDigits = 6, randomNum;
int[] userNum = new int[numDigits];
int[] lotteryNumbers = new int[numDigits];
for (int i = 0; i < 6; i++) {
randomNum = (int) (Math.random() * 45);
for (int x = 0; x < i; x++) {
if (lotteryNumbers[x] == randomNum || lotteryNumbers[x] == 0) {
randomNum = (int) (Math.random() * 45);
x = -1;
}
}
lotteryNumbers[i] = randomNum;
}

for (int i = 0; i < userNum.length; i++) {
System.out.print("Enter your numbers: ");
userNum[i] = keyIn.nextInt();
System.out.println("Your number is: " + userNum[i]);
System.out.println(" ");
}
keyIn.close();

System.out.println("You've entered the following numbers...");
System.out.println(Arrays.toString(userNum));

System.out.println(" ");
System.out.println("The lottery numbers are: ");
for (int i = 0; i < 6; i++) {
Arrays.sort(lotteryNumbers);
System.out.print(+lotteryNumbers[i] + " ");
}


Revision Question 9

I am trying to implement the matching part of the question but I ran into some issues, it is asking me to look for matches in the columns rather than rows.

I need to know how to check if the numbers match in the column.

My attempt at it.

for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++)
{
if(lotteryNumbers[i] == userNum[j])
{
match++;
}
}
}
System.out.println(" ");
System.out.print("Matches: " +match +" found.");


But this looks for matches in the rows, I would like to make it so it looks for matches by column

Answer
   for(int i=0;i<6;i++){
        for(int j=i;j<6;j++){
            if(userNum[i]==lotterNumbers[j]){
                System.out.println("match found: "+userNum[i]);
            }
        }
    }

this code looks if you have matching numbers in user input and random number and if so prints match found + number

array as row: 1|2|3|4|5|6|7

array as column:

1

2

3

4

5

6

if you look at an array like a row you match by columns otherwise by row