拒绝某些数字的程序

时间:2017-05-09 14:51:17

标签: java

我需要创建一个程序,在1到42之间随机打印6个数字,其中没有2个数字是相同的。用户还必须插入6个数字。如果任何数字与计算机随机选择的数字相同,则计算机必须打印它。如果没有,电脑打印你就是这样一个失败者。现在,问题是我不确定如何确保没有2个随机选择的数字是相同的。如果数字小于1,大于42或等于插入的前一个数字,程序也应该要求一个不同的数字,并扫描它我也无法做到。 (用户不能输入2个相同的数字)

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

public class LotoMachine {

public static void main(String[] args) {

    System.out.println("Please enter 6 numbers between 1 and 42.");
    Scanner scan = new Scanner(System.in);

    int[] marks = new int[6];
    Random ran = new Random();
    int[] x = new int[6];
    boolean winner = false;

    for (int i = 0; i < 6; i++) {
        marks[i] = scan.nextInt();
        while (marks[i] > 42) {
            System.out.println(marks[i] + " is out of range. Please pick a number that is less than 43.");
            marks[i] = scan.nextInt();
            i=0;

        }
        while (marks[i] < 1) {
            System.out.println(marks[i] + " is out of range. Please pick a number that is greater than 0.");
            marks[i] = scan.nextInt();
            i=0;
        }
        while (marks[i] == marks[i] - 1) {
            System.out.println("You have already chosen " + marks[i] + "Please pick a different number.");
            marks[i] = scan.nextInt();
            i=0;

        }
    }
    for (int j = 0; j < 6; j++) {
        x[j] = ran.nextInt(42) + 1;
        for (int y = 0; y < j; y++) {
            if (x[j] == x[y]) {
                x[j] = ran.nextInt(42) + 1;
                j = 0;

            }
        }
    }
    System.out.print("You chose");
    for (int m = 0; m < 6; m++) {
        System.out.print(" " + marks[m]);
    }
    System.out.println(" ");
    System.out.print("The random numbers are");
    for (int m = 0; m < 6; m++) {
        System.out.print(" " + x[m]);
    }
    System.out.println(" ");
    System.out.print("The number(s) that matched are");
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 6; j++) {
            if (marks[i] == x[j]) {
                winner = true;
                System.out.print(" " + marks[i]);
            }
        }
    }
    if (winner != true) {
        System.out.println("You are such a loser");

    }
}

}

2 个答案:

答案 0 :(得分:2)

您可以使用Set来存储值,并在添加后询问大小。每当你想处理不应该重复的对象集合时,Set是一个很好的方法,因为它不允许重复。 像这样:

Set<Integer> numbers = new HashSet<Integer>();

random code goes here...

int size = numbers.size();
numbers.add(newValue);
if(numbers.size() == size){
    number needs to be created again...
}

答案 1 :(得分:0)

基本上,有两种方法可以从42中绘制6。

第一种是连续绘制,直到你有6个唯一的数字。每次抽奖具有相同的概率(1/42)以绘制特定数字。虽然你总是绘制相同数字的情况的可能性很低,但它不是0.所以从统计的角度来看,这是不正确的。在Java中你可以做到这一点

Random rand = new Random();
List<Integer> randomNumbers = Stream.generate(() -> rand.nextInt(42))
                                    .distinct()
                                    .limit(6)
                                    .collect(toList());

你真正想要的是绘制42中的1个,41个中的1个,40个中的1个...这样做的一种方法是生成所有可能数字的列表(1..42),绘制一个,将其从列表中删除,绘制另一个等。在Java中将是

final List<Integer> numbers = IntStream.rangeClosed(1, 42)
                                       .boxed()
                                       .collect(toList());
final List<Integer> randomNumbers2 = Stream.generate(() -> rand.nextInt(numbers.size()))
                                           .limit(6)
                                           .map(i -> numbers.remove((int)i))
                                           .collect(toList());

最后读取输入,直到你有一个有效的数字很容易用一个do-while循环

Scanner scanner = new Scanner(System.in);
int number;
do{
  number = scanner.nextInt();
} while(number < 0 && number > 42);

if(randomNumbers2.contains(number)){
  System.out.println("Winner");
} else {
  System.out.println("Looser");
}