我遇到了Java分配问题。我需要构建一个程序,允许某人从彩票中输入6个数字。然后,用户将从其票证中输入另外6个号码。然后程序将检查该抽奖的匹配数量。任何少于3场比赛的门票都不是赢家。我需要提供一条消息,说明是否有3,4,5或6个匹配项。我在课程笔记中找不到任何相关内容。下面是我到目前为止编写的代码:
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
}
}
答案 0 :(得分:1)
f1zz0_13&#39的回答是有效的。
如果数字必须相同且位于相同位置,则执行以下操作:
//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...
答案 1 :(得分:0)
int count = 0;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
if (array1[i] == yourNumbers[j]) {
count++;
break;
}
假设6个彩票号码是唯一的,在您输入输入后添加这段代码应该会给出与变量count
中的彩票号码匹配的数量。