用java设计一个双色球程序:双色球每注投号码和1个蓝色球号码和6个红色球号码组成。需要自行输入6个红色球号码和1个蓝色球号码共7个号码。红色球号码随机从1~33中选择6个号码且不重复,每中一个号码获得50元;蓝色球号码从1~16中随机选择,中奖获得200元;要求:7个号码用集合TreeSet保存,最后按顺序打印单号,订单号包括随机产生的7个号码(6个蓝色球号码和1个蓝色球号码)和自行输入的7个号码。
时间: 2024-02-24 13:53:06 浏览: 235
好的,以下是Java代码实现:
```java
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;
public class DoubleColorBall {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入6个红色球号码(1~33之间):");
TreeSet<Integer> redSet = new TreeSet<Integer>();
while (redSet.size() < 6) {
int num = scanner.nextInt();
if (num >= 1 && num <= 33) {
redSet.add(num);
} else {
System.out.println("请输入1~33之间的数字!");
}
}
System.out.println("请输入1个蓝色球号码(1~16之间):");
int blueNum = scanner.nextInt();
if (blueNum < 1 || blueNum > 16) {
System.out.println("请输入1~16之间的数字!");
return;
}
System.out.println("您选择的红色球号码为:" + redSet);
System.out.println("您选择的蓝色球号码为:" + blueNum);
TreeSet<Integer> lotterySet = new TreeSet<Integer>();
Random random = new Random();
while (lotterySet.size() < 6) {
int num = random.nextInt(33) + 1;
lotterySet.add(num);
}
int lotteryBlueNum = random.nextInt(16) + 1;
lotterySet.add(lotteryBlueNum);
System.out.println("本期开奖号码为:" + lotterySet);
System.out.println("本期蓝色球号码为:" + lotteryBlueNum);
int redCount = 0;
for (int num : redSet) {
if (lotterySet.contains(num)) {
redCount++;
}
}
int winAmount = 0;
if (redCount == 6 && blueNum == lotteryBlueNum) {
winAmount = 10000000;
} else if (redCount == 6) {
winAmount = 5000000;
} else if (redCount == 5 && blueNum == lotteryBlueNum) {
winAmount = 3000;
} else if (redCount == 5 || (redCount == 4 && blueNum == lotteryBlueNum)) {
winAmount = 200;
} else if (redCount == 4 || (redCount == 3 && blueNum == lotteryBlueNum)) {
winAmount = 10;
} else if (blueNum == lotteryBlueNum) {
winAmount = 5;
}
System.out.println("您中了:" + winAmount + "元!");
System.out.println("本期开奖号码为:" + lotterySet);
System.out.println("您选择的号码为:" + redSet + ", " + blueNum);
}
}
```
程序中使用了一个 `TreeSet` 来保存号码,这样可以自动去重并且按照升序排序。先让用户输入6个红色球号码和1个蓝色球号码,然后用随机数生成本期的7个号码(6个红色球号码和1个蓝色球号码),并且计算用户中奖的金额。最后打印出本期开奖号码和用户选择的号码。
阅读全文