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

时间:2017-03-21 16:00:26

标签: 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

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

3 个答案:

答案 0 :(得分:1)

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

我是这样做的:

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;
    }
}

答案 1 :(得分:1)

实际上,您的冒泡排序和重复检查都不起作用。

对于重复检查,如果检测到重复,则生成新的随机数但没有任何内容阻止您的代码生成相同的副本。尝试将if (num[x] == num[y])更改为while (num[x] == num[y])

对于冒泡排序,我认为你误解了算法。一般形式应为:

boolean changed = false;
do {
    for (int i = 0; i < size - 1; i++) {
        if (num[i] > num[i+1]) {
            // swap 
            changed = true;
        }
    }
} while (changed);

虽然我认为它超出了你的问题的范围,但整个集合可以用Java 8在一个语句中生成:

int[] nums = random.ints(1, 50).distinct().limit(6).sorted().toArray();

这被解释为:生成1到49之间的无限随机数流,删除重复项,获取前6个,对它们进行排序并将它们作为数组返回。

答案 2 :(得分:1)

这是我的版本:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

class LotoTicket {
  private static final int NUM_NUMBERS = 6;
  private static final int NUM_BALLS = 99;
  private static final Random random = new Random();
  private final int[] numbers;

  private LotoTicket(final int[] numbers) {
    this.numbers = numbers;
  }

  public static LotoTicket generateTicket() {
    final int[] numbers = new int[NUM_NUMBERS];
    final List<Integer> ballPool = new ArrayList<Integer>();
    for (int i = 0; i < NUM_BALLS; i++) {
      ballPool.add(i, i + 1);
    }
    for (int i = 0; i < NUM_NUMBERS; i++) {
      final int draw = random.nextInt(NUM_BALLS - i);
      numbers[i] = ballPool.remove(draw);
    }
    bubbleSort(numbers);
    return new LotoTicket(numbers);
  }

  private static void bubbleSort(final int[] numbers) {
    if (numbers.length <= 1) {
      return;
    }
    boolean sorted;
    do {
      sorted = true;
      for (int i = 1; i < numbers.length; i++) {
        if (numbers[i - 1] > numbers[i]) {
          final int temp = numbers[i];
          numbers[i] = numbers[i - 1];
          numbers[i - 1] = temp;
          sorted = false;
        }
      }
    } while (sorted == false);
  }

  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder("LotoTicket{");
    sb.append("numbers=").append(Arrays.toString(numbers));
    sb.append('}');
    return sb.toString();
  }

  public static void main(final String... args) {
    System.out.println(LotoTicket.generateTicket());
    System.out.println(LotoTicket.generateTicket());
    System.out.println(LotoTicket.generateTicket());
  }
}

修改

这是一个符合java1.5的版本,它实现了一个简单的bubble-sort:

{{1}}