gpt4 book ai didi

java - 如何修复java中随机数 boolean 数组的: Exception in thread "main" java. lang.ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-02 01:38:12 27 4
gpt4 key购买 nike

我正在编写一个程序,可以获取他们想要的随机数以及他们想要达到的最高数字,但是当我运行代码时,它没有给我他们要求的数字数量。它要么给我 1 个号码,要么没有。然后它说“线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException: 9 在practice4.practice4.main(practice4.java:38)”处为红色​​。

double randomNum;
int highestNumber, numsInLottery, display;
boolean[] numberUsed;

System.out.println("Welcome to the Lottery Number Generator!");

System.out.println("How many numbers are in your lottery?");
numsInLottery = TextIO.getInt();

System.out.println("What is the highest possible number?");
highestNumber = TextIO.getInt();


numberUsed= new boolean [numsInLottery];

for (int index = 0; index < numsInLottery; index++)
{
numberUsed [index]= false;

while(index < highestNumber)
{
do
{

randomNum = Math.random() * highestNumber + 1;
display = (int) randomNum ;

} while (numberUsed [display - 1 ] );

System.out.print(display + " ");
numberUsed [display + 1] = true;

}
}
}
}

在我有numberUsed= new boolean [numsInLottery];之前我不小心把highestNumber 放在了numsInLottery 的位置。当我有了它时,它会给我所有的数字。但现在我已经改变了它,它不再起作用了这就是我现在得到的

Welcome to the Lottery Number Generator!
How many numbers are in your lottery?
6
What is the highest possible number?
? 30
1 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 21
at practice4.practice4.main(practice4.java:35)

最佳答案

在您的情况下,您似乎需要使用 boolean 数组来验证之前是否未生成过生成的数字,在这种情况下,这就是您需要的核心逻辑。

int[] lotteryNumbers = new int[numsInLottery];
boolean[] lotteryNumberHasBeenGenerated = new boolean[highestNumber];

for(int i=0;i<highestNumber;i++){ //none of the numbers have been generated at this point
lotteryNumberHasBeenGenerated[i]=false;
}

for (int i = 0; i < numsInLottery; i++) {
int random;
do{
random=((Double)(Math.random() * highestNumber)).intValue();
}while(lotteryNumberHasBeenGenerated[random]);

lotteryNumbers[i] = random;
lotteryNumberHasBeenGenerated[random]=true; //the number is maked as generated

}

这将解决问题,最终的方法应该如下所示:

public void lottery() {

System.out.println("Welcome to the Lottery Number Generator!");
System.out.println("How many numbers are in your lottery?");
int numsInLottery = TextIO.getInt();
System.out.println("What is the highest possible number?");
int highestNumber = TextIO.getInt();

int[] lotteryNumbers = new int[numsInLottery];
boolean[] lotteryNumberHasBeenGenerated = new boolean[highestNumber];

for(int i=0;i<highestNumber;i++){ //none of the numbers have been generated at this point
lotteryNumberHasBeenGenerated[i]=false;
}

for (int i = 0; i < numsInLottery; i++) {
int random;
do{
random=((Double)(Math.random() * highestNumber)).intValue();
}while(lotteryNumberHasBeenGenerated[random]);

lotteryNumbers[i] = random;
lotteryNumberHasBeenGenerated[random]=true; //the number is maked as generated

}

System.out.println("Lottery numbers:\n" + Arrays.toString(lotteryNumbers));
}

尽管如果是这种情况,您将错过很多验证:S

Also, The problem you are having in the console logs is thrown in java when you try to access a position in the array that it does not have, for example if you created an array with size 5 and you are accessing the position number 9 or with index 8 Exception. Here is an explanation of how that works

关于java - 如何修复java中随机数 boolean 数组的: Exception in thread "main" java. lang.ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54877907/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com