Java:创建简单的彩票号码生成器

标签 java

我必须创建一个以 Lotto 6/49(加拿大安大略省)为模型的简单 Java 彩票号码生成器。用户被询问他们希望生成多少张票。门票必须按编号升序排列,且不得重复。例如,如果我想要 3 张票,输出可能是:

8 12 17 25 32 47
6 10 21 30 39 42
1 8 16 37 45 49

尝试对数字进行排序时出现问题。我们被教导使用冒泡排序,但是我的重复检查无法正常工作,所以我最终会得到如下输出:

8 18 29 29 29 29
4 12 18 18 24 24
4 12 18 24 46 46

我的代码如下:

// The "Lotto" class.
import java.awt.*;
import hsa.Console;

public class Lotto
{
    static Console c;           // The output console

public static void main (String[] args)
{
    c = new Console ();
    int t = 0;
    int num[];
    num = new int[10];

    c.println("How many Lotto 6/49 tickets do you wish to generate?");
    t = c.readInt();
    c.println("****************");

    for (int a = 1; a <= t; a++)
    {
        for (int i = 1; i <= 6; i++)
        {
            num[i] = (int)(Math.random() * 49 + 1);
            for (int x = 1; x <= 6; x++) // duplicate check
            {
                for (int y = x + 1; y <= 7; y++)
                {
                    if (num[x] == num[y])
                    {
                        num[y] = (int)(Math.random() * 49 + 1);
                    }
                }
            } // end check
            for (int p = 1; p <=6; p++) // start sort
            {
                for (int q = 1; q <=7; q++)
                {
                    if (num[p] < num[q])
                    {
                        int temp = num[p];
                        num[p] = num[q];
                        num[q] = temp;
                    }
                }
            } // end sort
            c.print(num[i] + " ");
        }
    c.println();
    }       
} // main method
} // Lotto class

任何对此事的帮助或解决方案将不胜感激。谢谢!

最佳答案

这是一个糟糕的抽象。我建议将所有逻辑嵌入到类中。面向对象编程是关于抽象、封装和隐藏细节的。

我的做法是这样的:

package gambling;

import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by Michael
 * Creation date 3/21/2017.
 * @link https://stackoverflow.com/questions/42932262/java-creating-simple-lottery-number-generator
 */
public class LottoTicket {

    public static final int DEFAULT_NUM_TICKETS = 10;
    public static final int DEFAULT_MAX_VALUE = 49;
    public static final int DEFAULT_NUM_VALUES = 6;
    private Random random;

    public static void main(String[] args) {
        int numTickets = (args.length > 0) ? Integer.parseInt(args[0]) : DEFAULT_NUM_TICKETS;
        LottoTicket lottoTicket = new LottoTicket();
        for (int i = 0; i < numTickets; ++i) {
            System.out.println(lottoTicket.getNumbers(DEFAULT_NUM_VALUES, DEFAULT_MAX_VALUE));
        }
    }

    public LottoTicket() {
        this(null);
    }

    public LottoTicket(Long seed) {
        this.random = (seed != null) ? new Random(seed) : new Random();
    }

    public Set<Integer> getNumbers(int numValues, int maxValue) {
        Set<Integer> numbers = new TreeSet<>();
        while (numbers.size() < numValues) {
            numbers.add(this.random.nextInt(maxValue) + 1);
        }
        return numbers;
    }
}

关于Java:创建简单的彩票号码生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42932262/

相关文章:

java - 如何从java中的对象数组中检索特定对象

java - 在 Spring Boot 中看似随机调用/错误

java - 如果我们使用工厂模式,我们是否应该向用户隐藏产品?

java - JSP 将 XML 数据发送到服务器

java - Selenium - 单击仅在将鼠标悬停在其上方时出现的隐藏按钮

java - log4j配置文件和jar应用程序

java - Google Cloud 端点和身份验证

java - 如何有效地从文件中解析串联的 XML 文档

java - Spymemcached 与 hibernate-memcached 和 webapp-runner 冲突

java - com.sun.glass 不存在