I\'m trying toassign five numbers to an array without duplicating a number. Thec
ID: 3618549 • Letter: I
Question
I'm trying toassign five numbers to an array without duplicating a number. Thecode I have doesn't work for some reason. I know why it is out ofbounds, but it is still assigning duplicatenumbers sometimes.private static int[] numbersArray = {1, 2, 3, 4, 5, 6, 7, 8, 9,0};
while (programNumArray[i] != randomNum)//;do
{
for (i = 0; i <= 5; i++)
{
randomNum =generator.nextInt(numbersArray.length);
programNumArray[i] = randomNum;
System.out.println(programNumArray[i]);
}
} I'm trying toassign five numbers to an array without duplicating a number. Thecode I have doesn't work for some reason. I know why it is out ofbounds, but it is still assigning duplicatenumbers sometimes.
private static int[] numbersArray = {1, 2, 3, 4, 5, 6, 7, 8, 9,0};
while (programNumArray[i] != randomNum)//;do
{
for (i = 0; i <= 5; i++)
{
randomNum =generator.nextInt(numbersArray.length);
programNumArray[i] = randomNum;
System.out.println(programNumArray[i]);
}
}
Explanation / Answer
As far as I can tell, your program either: 1) Won't run at all: this will occur if randomNumber andprogramNumArray[i] are equivalent when you reach the whileloop. 2) Will run forever. The latter will occur because your program is set to stop whenprogramNumArray[i] is not equal torandomNum. Here are your problems: 1) Except for the first iteration, your loop will always comparerandomNum to programNumArray[6], theseventh element of the (zero-indexed)programNumArray. Your for loop is exited when iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.