创建自动彩票

时间:2016-09-20 01:29:15

标签: java

我想编制一个自动化的彩票"脚本"它创建了2个阵列:一个持有彩票号码,另一个持有猜测本身。然后,我检查是否例如。 lottery []包含guess []的数字,如果数字相等并且他在循环中命中了6个正确的数字,我希望控制台打印出他需要多少次尝试。

public static void main(String[] args) {

    int[] lottery = new int[6];
    int[] tipp1 = new int[6];
    int randomNum;
    int tipp;

    for (int i = 0; i < 6; i++) {
        randomNum = (int) (Math.random() * 49 + 1); // Random number created here.
        for (int x = 0; x < i; x++) {
            if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
            {
                randomNum = (int) (Math.random() * 49 + 1);// If random number is same, another number generated.
                x = -1; // restart the loop
            }
        }
        lottery[i] = randomNum;
        System.out.print(lottery[i] + " ");
    }

    System.out.println(" ");

    for (int c = 0; c < 6; c++) {
        tipp = (int) (Math.random() * 49 + 1); // Random number created here.
        for (int xy = 0; xy < c; xy++) {
            if (tipp1[xy] == tipp) // Here, code checks if same random number generated before.
            {
                tipp = (int) (Math.random() * 49 + 1);// If random number is same, another number generated.
                xy = -1; // restart the loop
            }
        }
        tipp1[c] = tipp;
        System.out.print(tipp1[c] + " ");
    }
    if (lottery.equals(tipp1)){
        System.out.print("HIT!");
    }
}
}

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:3)

所以我理解你的问题的方式如下:你想要一个程序,生成一组6个随机数,每个数字在1到50的范围内 - 这代表正确的彩票号码。然后,您希望程序生成6个数字的随机集合,每个数字在1到50的范围内,直到其中一个集合与彩票号码匹配。您还希望程序能够跟踪在获胜之前进行了多少次猜测 - 我假设这意味着一次选择抽奖号码并且不断猜测直到匹配发生。

注意:我没有在代码示例中看到用于跟踪猜测数量的逻辑,但我确实在下面的代码中实现了此功能。

目前尚不清楚彩票号码的顺序是否重要。我编写了下面的代码,这样订单并不重要,这意味着只要猜测包含所有6个彩票号码,它就会被视为赢家。如果您希望订单变得重要,则应移除对Arrays.sort(...)

的调用

以下是使用数组的解决方案,因为您明确表示要使用数组:

import java.util.Arrays;
import java.util.Random;

public class LotteryMain {
    public static void main(String[] args) {

        int[] correctNumbers = createSortedArrayOfRandomInts(6);
        System.out.println("Lottery numbers: "+Arrays.toString(correctNumbers));

        int tries = 0;
        int[] guessNumbers = null;
        do{
            guessNumbers = createSortedArrayOfRandomInts(6);
            System.out.println("Guessed numbers: "+Arrays.toString(guessNumbers));
            tries++;
        }while(!Arrays.equals(guessNumbers, correctNumbers));

        System.out.println("Lottery numbers: "+Arrays.toString(correctNumbers));
        System.out.println("WINNER! Number of tries: "+tries);
    }

    public static int[] createSortedArrayOfRandomInts(int sizeOfArray){
        int[] arrayOfRandomNumbers = new int[sizeOfArray];
        Random randomGen = new Random();
        for (int i = 0; i < sizeOfArray; i++) {
            int randomNum = -1;
            do{
                randomNum = randomGen.nextInt(50)+1; // Random number created here.
            }while(contains(arrayOfRandomNumbers, randomNum));
            arrayOfRandomNumbers[i] = randomNum;
        }
        Arrays.sort(arrayOfRandomNumbers);
        return arrayOfRandomNumbers;
    }

    public static boolean contains(int[] array, int searchFor){
        for(int i = 0; i < array.length; i++){
            if(array[i] == searchFor)
                return true;
        }
        return false;
    }

}

以下是示例输出(显然缩短了):

...
Guessed numbers: [5, 21, 27, 36, 40, 47]
Guessed numbers: [3, 14, 15, 28, 36, 42]
Guessed numbers: [24, 27, 31, 32, 44, 49]
Guessed numbers: [19, 21, 25, 39, 42, 46]
Lottery numbers: [19, 21, 25, 39, 42, 46]
WINNER! Number of tries: 2499043

这是使用Java Collections Framework中的类的解决方案,因为每个Java程序员都应该熟悉如何使用这些类:

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

public class LotteryWithCollections {

    public static void main(String[] args) {

        List<Integer> correctNumbers = createSortedListOfRandomInts(6);
        System.out.println("Lottery numbers: " + correctNumbers);

        int tries = 0;
        List<Integer> guessNumbers = null;
        do {
            guessNumbers = createSortedListOfRandomInts(6);
            System.out.println("Guessed numbers: " + guessNumbers);
            tries++;
        } while (!guessNumbers.equals(correctNumbers));

        System.out.println("Lottery numbers: " + correctNumbers);
        System.out.println("WINNER! Number of tries: " + tries);
    }

    public static List<Integer> createSortedListOfRandomInts(int sizeOfList) {
        List<Integer> listOfRandomNumbers = new ArrayList<Integer>();
        Random randomGen = new Random();
        for (int i = 0; i < sizeOfList; i++) {
            int randomNum = randomGen.nextInt(50) + 1; // Random number created
                                                        // here.
            while (listOfRandomNumbers.contains(randomNum)) {
                randomNum = randomGen.nextInt(50) + 1;
            }
            listOfRandomNumbers.add(randomNum);
        }
        Collections.sort(listOfRandomNumbers);
        return listOfRandomNumbers;
    }
}

还需要考虑其他事项 - 现在代码允许重复猜测,跟踪以前的猜测并禁止重复相同猜测可能会很有趣。

答案 1 :(得分:0)

所以我觉得我刚刚为你完成了你的功课......但这里有一些源代码可以完成我认为你的目标。我不知道你的乐透的确切限制。

您需要了解DRY,如我在使用随机数组静态方法所证明的评论中所述。另外,根据您的源代码,您应该了解java中的变量作用域。如果你想要我的源代码运行,直到在循环逻辑中有一个非常简单的命中。

package lotto;
/**
 * @author czarking
 */
public class Lotto {

    private static int[] lottery = new int[6];
    private static int[] guess = new int[6];

    public static void main(String[] args) {
        lottery = randomArray(lottery);
        guess = randomArray(lottery);
        boolean match = true;
        for (int i=0; i<lottery.length; i++) {
            if (lottery[i] != guess[i])
                match = false;
        }
        if (match)
            System.out.println("HIT!");
    }

    public static int[] randomArray(int[] ar) {
        for (int i=0; i<ar.length; i++) {
            ar[i] = (int)(Math.random()*49 + 1);
            System.out.println(ar[i]+" ");
        }
        return ar;
    }

}