You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何替换Java数组中的重复数字?彩票程序去重需求求助

解决Java彩票程序数组前5位数字重复问题

我为学校项目编写了一个可打印用户指定数量彩票的Java程序,程序运行正常,但存在数组内数字重复的问题。教授要求确保数组前5个数字无重复,请问如何遍历数组、检测重复数字并将其替换为不同的随机数?例如数组{27,22,34,15,15}应修改为{27,22,34,15,12}。

当前程序代码

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

/**
* SuperLotto program prompts user for number
* of lottery tickets, printing them as requested
* 
* @author j
*/
public class SuperLotto {   

   /**
   * This method declares an array numberList
   * containing 6 elements
   */
   public static void quickPick() {
    int numberList[] = new int[6];
    
    //calls the printTicket method for the array
    printTicket(numberList);
}


/**
 * This method iterates through array to print the elements
 * 
 * @param numberList - this parameter accepts the array
 */
public static void printTicket(int[] numberList) {
    Random rnd = new Random();
    
    /* method assigns random numbers to first 
     * 5 elements in the array and prints them
     */
    for (int i = 0; i < 5; i++) {
    System.out.print(rnd.nextInt(((27-1) + 1) + 1) + " ");
    }
    
    /* method assigns random number to last element
     * in array and prints it in special format
     */
    for (int i = 0; i < 1; i++) {
        System.out.print("(MEGA: " + rnd.nextInt(((27-1) + 1) + 1) + ")");
    }
    
}


//main method
public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    String name;
    int numberOfTickets;
    String decide;
    
    //Program prompts the user for their name
    System.out.println("Welcome to the Quicky Mart, what is your name?");
    //User inputs their name
    name = scnr.next();
    
    //new line
    System.out.println();
    
    //Program prompts the user for number of tickets
    System.out.println("Hi " + name + ", how many Super Lotto "
            + "tickets would you like to purchase?");
    //User inputs the number of tickets
    numberOfTickets = scnr.nextInt();
    
    //new line
    System.out.println();
    
    /* Program outputs the number
     * of tickets entered by user
     */
    System.out.println("Super Lotto Ticket(s): ");
    for (int i = 0; i < numberOfTickets; i++) {
        quickPick();
        System.out.println();
    }       
    
    //new line
    System.out.println();
    
    /* System prompts user if they would
     * like to run the program again
     */
    System.out.println("Good luck! "
            + "Would you like to run the program again?");
    
    //User enters their decision
    decide = scnr.next();
    
    /* If user decides yes,
     * main program runs again
     */
    while (decide.equalsIgnoreCase("yes")) {
        System.out.println();
        main(null);
        break;
    }
    
    /* If user decides no,
     * program terminates with a message
     */
    if (decide.equalsIgnoreCase("no")) {
        System.out.println();
        System.out.println("Goodbye!");
    }
    
  }
}

解决方案

原程序的问题在于直接生成随机数就打印,没有将数值存入数组也未做重复检测。要实现前5位数字无重复,需要先将随机数存入数组,每生成一个新数就检查是否与已存入的数字重复,重复则重新生成,直到得到不重复的数值。

修改后的完整代码如下:

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

/**
* SuperLotto program prompts user for number
* of lottery tickets, printing them as requested
* 
* @author j
*/
public class SuperLotto {   

   public static void quickPick() {
    int numberList[] = new int[6];
    printTicket(numberList);
}


public static void printTicket(int[] numberList) {
    Random rnd = new Random();
    final int MAX_NUM = 27; // 定义随机数范围上限,简化代码
    
    // 填充前5位无重复数字
    for (int i = 0; i < 5; i++) {
        int num;
        boolean isDuplicate;
        do {
            isDuplicate = false;
            num = rnd.nextInt(MAX_NUM) + 1; // 生成1-27的随机数
            // 检查当前数字是否与已存入数组的数字重复
            for (int j = 0; j < i; j++) {
                if (numberList[j] == num) {
                    isDuplicate = true;
                    break;
                }
            }
        } while (isDuplicate); // 如果重复,重新生成
        numberList[i] = num;
        System.out.print(num + " ");
    }
    
    // 生成并打印最后一位MEGA数字
    int megaNum = rnd.nextInt(MAX_NUM) + 1;
    numberList[5] = megaNum;
    System.out.print("(MEGA: " + megaNum + ")");
    
}


public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    String name;
    int numberOfTickets;
    String decide;
    
    System.out.println("Welcome to the Quicky Mart, what is your name?");
    name = scnr.next();
    
    System.out.println();
    
    System.out.println("Hi " + name + ", how many Super Lotto "
            + "tickets would you like to purchase?");
    numberOfTickets = scnr.nextInt();
    
    System.out.println();
    
    System.out.println("Super Lotto Ticket(s): ");
    for (int i = 0; i < numberOfTickets; i++) {
        quickPick();
        System.out.println();
    }       
    
    System.out.println();
    
    System.out.println("Good luck! "
            + "Would you like to run the program again?");
    
    decide = scnr.next();
    
    while (decide.equalsIgnoreCase("yes")) {
        System.out.println();
        main(null);
        break;
    }
    
    if (decide.equalsIgnoreCase("no")) {
        System.out.println();
        System.out.println("Goodbye!");
    }
    
  }
}

关键修改点说明

  • 重复检测逻辑:在生成前5位数字时,使用do-while循环,每次生成新数字后,遍历数组中已存入的元素,判断是否重复。如果重复则重新生成,直到得到不重复的数字。
  • 代码简化:将随机数范围(1-27)定义为常量MAX_NUM,避免重复计算((27-1)+1)+1,提升代码可读性。
  • 数组存储:现在生成的数字会先存入数组,再进行打印,符合题目中对数组操作的要求。

内容的提问来源于stack exchange,提问作者Jarad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 20:15:43