打好基础,daydayup!
题目:要求如下(同时:红球每个号码不可以相同)
?
编程思路:1,创建一个可以录入数字的数组;2,生成一个可以随机生成数字的数组;3,两个数组进行比较,统计相同数字。
public class shuangseqiudemo {
public static void main(String[] args) {
int[] xuanhao = la();
System.out.println("你的号码为:");
ll(xuanhao);
int[] zhongjiang = lol();
System.out.println("中奖号码为:");
ll(zhongjiang);
z(xuanhao, zhongjiang);
}
//1,创建一组数据
public static void ll(int[] la) {
System.out.print("[");
for (int i = 0; i < la.length; i++) {
System.out.print(i == la.length - 1 ? la[i] : la[i] + ",");
}
System.out.println("]");
}
public static int[] la() {
int[] la = new int[7];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < la.length - 1; i++) {
while (true) {
System.out.println("请第" + (i + 1) + "次选号(1-33):");
int num = sc.nextInt();
if (num > 33 || num < 1) {
System.out.println("请重新输入");
} else {
if (wxd(la, num)) {
System.out.println("不可以重复,请重新输入");
} else {
la[i] = num;
break;
}
}
}
}
while (true) {
System.out.println("输入最后一个号码(1-16):");
int num1 = sc.nextInt();
if (num1 < 1 || num1 > 16) {
System.out.println("请重新输入");
} else {
la[la.length - 1] = num1;
break;
}
}
return la;
}
private static boolean wxd(int[] la, int num) {
for (int i = 0; i < la.length; i++) {
if (la[i] == num) {
return true;
}
}
return false;
}
//2,创建中奖号码
public static int[] lol() {
int[] lol = new int[7];
Random r = new Random();
for (int i = 0; i < lol.length - 1; i++) {
while (true) {
int num2 = r.nextInt(33) + 1;//减加法 33 ==0-32 +1
if (!wxd(lol, num2)) {
lol[i] = num2;
break;
}
}
}
lol[lol.length - 1] = r.nextInt(16) + 1;
return lol;
}
//3,匹配中奖情况
public static void z(int[] xuanhao, int[] zhongjiang) {
int redcount = 0;
int bucount = 0;
for (int i = 0; i < zhongjiang.length - 1; i++) {
for (int j = 0; j < xuanhao.length - 1; j++) {
if (xuanhao[j] == zhongjiang[i]) {
redcount++;
break;
}
}
}
bucount = xuanhao[xuanhao.length - 1] == zhongjiang[zhongjiang.length - 1] ? 1 : 0;
if (redcount == 6 && bucount == 1) {
System.out.println("一等奖1000万");
} else if (redcount == 6 && bucount == 0) {
System.out.println("二等奖1000万");
} else if (redcount == 5 && bucount == 1) {
System.out.println("三等奖3000元");
} else if (redcount == 5 && bucount == 0 || redcount == 4 && bucount == 1) {
System.out.println("四等奖200元");
} else if (redcount == 4 && bucount == 0 || redcount == 3 && bucount == 1) {
System.out.println("10元");
} else if (redcount < 3 && bucount == 1) {
System.out.println("5元");
} else {
System.out.println("谢谢参与");
}
}
}
?实验结果:完美运行
撒花!!!!
?