I am having trouble with a Java assignment. I need to construct a program that will allow someone to enter 6 numbers from a lottery. The user will then enter another 6 numbers from their ticket. The program will then check how many matches there are for that draw. Any ticket that has less than 3 matches is not a winner. I need to provide a message that states whether there are 3, 4, 5 or 6 matches. I cannot find anything in my course notes for this. Below is the code I have written so far:
import java.util.Scanner;
//Activates scanner utility
public class lottery
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
//Names scanner, completes activating scanner
int array1[] = new int [6];
System.out.print("Enter Winning Numbers:");
for(int j=0;j<array1.length;j=j+1){
array1[j] = in.nextInt();
}
System.out.print("Winning Numbers Are: ");
for(int j=0;j<array1.length;j=j+1){
System.out.print(array1[j]+" ");
}
int yourNumbers[] = new int [6];
System.out.println();
System.out.print("Enter Your Numbers:");
for(int j=0;j<yourNumbers.length;j=j+1){
yourNumbers[j] = in.nextInt();
}
System.out.println("Your Numbers Are: ");
for(int j=0;j<yourNumbers.length;j=j+1){
System.out.print(yourNumbers[j]+" ");
}
System.out.println();
if(array1==yourNumbers){
System.out.println("6 Numbers Match - JACKPOT WINNER!!!");;
} else {
System.out.print("Not a Winner - Better Luck Next Time.");
}
in.close();
//Deactivates the scanner utility
}
}
f1zz0_13's answer works if positioning doesn't matter.
If the numbers need to be the same and in the same position then do something like this:
//your code:
System.out.println("Your Numbers Are: ");
for(int j=0;j<yourNumbers.length;j=j+1){
System.out.print(yourNumbers[j]+" ");
}
//new stuff:
System.out.println();
int similar = 0;
for(int i = 0; i < array1.length; i++) {
if(array1[i] == yourNumbers[i]) {
similar++;
}
}
if (similar >= 3) {
System.out.println(similar + " Numbers Match - JACKPOT WINNER!!!");
}
else {
System.out.print("Not a Winner - Better Luck Next Time.");
}
//close the scanner then end the function...
int count = 0;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
if (array1[i] == yourNumbers[j]) {
count++;
break;
}
Assuming the 6 lottery numbers are unique, adding this bit of code after you have taken the input should give you how many numbers match with the lottery numbers in the variable count
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.