我想编写一个自动彩票“ script”程序,该脚本创建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!");
}
}
}
有没有办法做到这一点?
最佳答案
因此,我理解您的问题的方式如下:您想要一个程序,该程序生成一组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;
}
}
还有其他需要考虑的问题-现在代码允许重复的猜测,跟踪先前的猜测并禁止重复相同的猜测可能很有趣。