投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1-33中选择;蓝色球号码从1-16中选择。
双色球中奖条件和奖金表:
代码如下:
package org.example;
import java.util.Random;
import java.util.Scanner;
public class Two_color {
public static void main(String[] args) {
int arr[] = Winningnumbers();//获取随机数,也就是中奖号码
//提示中奖号码
// for (int i = 0; i < arr.length; i++) {
// System.out.print(arr[i]+" ");
// }
int Usernumber[] = usernumber();//获取用户输入的号码
int redcount = 0, bluecount = 0;
//统计有多少个号码中了
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < Usernumber.length - 1; j++) {
if (Usernumber[j] == arr[i]) {
redcount++;
break;
}
}
}
if (arr[6] == Usernumber[6]) bluecount++;
//输出中奖金额
if (redcount == 6 && bluecount == 1) {
System.out.println("恭喜中奖1000万元!");
} else if (redcount == 6 && bluecount == 0) {
System.out.println("恭喜中奖500万元!");
} else if (redcount == 5 && bluecount == 1) {
System.out.println("恭喜中奖3000元!");
} else if ((redcount == 5 && bluecount == 0) || (redcount == 4 && bluecount == 1)) {
System.out.println("恭喜中奖200元!");
} else if ((redcount == 4 && bluecount == 0) || (redcount == 3 && bluecount == 1)) {
System.out.println("恭喜中奖10元!");
} else if ((redcount == 2 && bluecount == 1) || (redcount == 1 && bluecount == 1) || (redcount == 0 && bluecount == 1)) {
System.out.println("恭喜中奖5元!");
} else {
System.out.println("谢谢参与!");
}
}
//接收用户号码
public static int[] usernumber() {
Scanner sc = new Scanner(System.in);
int number;
int arr[] = new int[7];
for (int i = 0; i < 6; ) {
System.out.println("请输入第" + (i + 1) + "个红球号码:");
number = sc.nextInt();
//先判断号码是否超出范围
if (number >= 1 && number <= 33) {
//判断号码是否重复
if (judgment(arr, number)) {
arr[i] = number;
i++;
} else {
System.out.println("号码重复");
}
} else {
System.out.println("号码超出范围");
}
}
while (true) {
System.out.println("请输入蓝球号码:");
number = sc.nextInt();
if (number >= 1 && number <= 16) {
arr[6] = number;
break;
} else {
System.out.println("号码超出范围");
}
}
return arr;
}
//获取中奖号码
public static int[] Winningnumbers() {
Random r = new Random();
int[] arr = new int[7];
for (int i = 0; i < arr.length - 1; ) {
int number = r.nextInt(33) + 1;
//判断号码是否重复
if (judgment(arr, number)) {
arr[i] = number;
i++;
}
}
arr[6] = r.nextInt(16) + 1;
return arr;
}
//判断数组中是否有重复
public static boolean judgment(int[] arr, int number) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == number) {
return false;
}
}
return true;
}
}
以上是学习Java中做的第一个有意思的小游戏,记录一下