随笔写的一个彩票系统,但是没有写奖项判定

mrwrq / 2024-09-02 / 原文

package Test;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class PriceSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList RedList = new ArrayList<>(); //记录红球的号码
ArrayList BlueList = new ArrayList<>(); //记录蓝球的号码
for (int i = 0; i < 6; i++) {
while (true) {
System.out.println("请输入第"+(i+1)+"个红球的号码:");
int redBall = sc.nextInt();
if (redBall <= 0 || redBall > 36) {
System.out.println("你输入的红球号码不存在,请重新输入!");
continue;
}
boolean redRepeat = isRedRepeat(RedList, redBall);
if (!redRepeat) {
System.out.println("你输入的红球已存在,请重新输入!");
continue;
}
break;
}
}

    for (int i = 0; i < 2; i++) {
        while (true){
            System.out.println("请输入第"+(i+1)+"个蓝球的号码:");
            int blueBall = sc.nextInt();
            if (blueBall <= 0 || blueBall > 18) {
                System.out.println("你输入的蓝球号码不存在,请重新输入!");
                continue;
            }
            boolean blueRepeat = isBlueRepeat(BlueList, blueBall);
            if (!blueRepeat) {
                System.out.println("你输入的蓝球已存在,请重新输入!");
                continue;
            }
            break;
        }
    }

    ArrayList<Integer> AllBall = getRedList(RedList, BlueList);
    Integer[] array = AllBall.toArray(new Integer[8]);

    for (int i = 0; i < array.length; i++) {
        System.out.print(array[i]+"\t");
    }

    System.out.println();

    int[] randomPrice = getRandomPrice();
    for (int i = 0; i < randomPrice.length; i++) {
        System.out.print(randomPrice[i]+"\t");
    }


    System.out.println();

    int red = RedBallCount(array, randomPrice);
    System.out.println("redCount: "+red);

    int blue = BlueBallCount(array, randomPrice);
    System.out.println("blueCount: "+blue);
}


public static boolean isRedRepeat(ArrayList<Integer> RedList,int redBall){ //检查红球是否重复
    for (Integer red : RedList) {
        if (red == redBall) {
            return false;
        }
    }
    RedList.add(redBall);
    return true;
}

public static boolean isBlueRepeat(ArrayList<Integer> BlueList,int blueBall){ //检查蓝球是否重复
    for (Integer blue : BlueList) {
        if (blue == blueBall) {
            return false;
        }
    }
    BlueList.add(blueBall);
    return true;
}

public static ArrayList<Integer> getRedList(ArrayList<Integer> RedList,ArrayList<Integer> BlueList) { //获取你输入的彩球信息
    ArrayList<Integer> list = new ArrayList<>();
    for (Integer RedBall : RedList) {
        list.add(RedBall);
    }

    for (Integer BlueBall : BlueList) {
        list.add(BlueBall);
    }

    return list;
}

public static int[] getRandomPrice(){ //生成随机的彩球中奖号数
    Random r = new Random();
    int[] price = new int[8];
    for (int i = 0; i < 8; i++) {
        if (i <=5){
            while (true){
                int ball = r.nextInt(36) + 1;
                boolean flag = repeat(ball, price);
                if (flag) {
                    price[i] = ball;
                    continue;
                }
                break;
            }
        }else {
            while (true){
                int ball = r.nextInt(16) + 1;
                boolean flag = repeat(ball, price);
                if (flag) {
                    price[i] = ball;
                    continue;
                }
                break;
            }
        }
    }
    return price;
}

public static boolean repeat(int onePrice,int[] price){ //检验随机生成的彩球中奖号是否有重复
    for (int i = 0; i < price.length; i++) {
        if (onePrice == price[i]) {
            return false;
        }
    }
    return true;
}

public static int RedBallCount(Integer[] array,int[] randomPrice){ //获取红球中奖号数
    int red = 0;
    for (int i = 0; i < array.length-2; i++) {
        for (int j = 0; j < randomPrice.length-2; j++) {
            if (array[i] == randomPrice[j]) {
                red++;
            }
        }
    }
    return red;
}


public static int BlueBallCount(Integer[] array,int[] randomPrice){ //获取中奖蓝球号数
    int blue = 0;
    for (int i = 6; i < array.length; i++) {
        for (int j = 6; j < randomPrice.length; j++) {
            if (array[i] == randomPrice[j]) {
                blue++;
            }
        }
    }
    return blue;
}

}