I am making a program where I get how many random numbers they want and the highest number they want to go up to, but when I run the code it does not give me the amount of numbers they asked for. it either gives me 1 number or none. Then it says "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9 at practice4.practice4.main(practice4.java:38)" in red.
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 + ] = true;
}
}
}
}
before I had numberUsed= new boolean [numsInLottery];
I accidentally put highestNumber where numsInLottery is. when I had that it would give me all the numbers. but now that I have changed it it does not work any more this is what I get now
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)
Aucun commentaire:
Enregistrer un commentaire