Here is my code and I can\'t see what I need to add to make this read 10 unique
ID: 441625 • Letter: H
Question
Here is my code and I can't see what I need to add to make this read 10 unique numbers between 1 and 100. I use i to go through and make each number random and j to check each number prior to i to see if it is the same, if it is, then i comes up with a new random number and j resets at 0 and checks them all again. import java.util.Arrays; public class randomb { public static void main(String[] args) { int[] random = new int[10]; for(int i = 0; i < 10; i++) { for(int j = 0; j < i; j++) { while(j == i); { random[i] = (int) (Math.random()*10) + 1; j=0; } } random[i] = (int) (Math.random()*10) + 1; } System.out.println(Arrays.toString(random)); } }Explanation / Answer
Hi,
Below code will give you desired output.
import java.util.Arrays;
public class randomb
{
public static void main(String[] args)
{
int[] random = new int[10];
int i,rand;
char flag;
i = 0;
flag = 'n';
while(i < 10)
{
rand = (int) (Math.random()*10) + 1;
for(int j = 0; j < i; j++)
{
if random[j] = rand
flag = 'y'
} /* End of for J loop */
if flag = 'n'
{
random[i] = rand;
i++;
}/* End of if loop */
} /* End of while I loop */
System.out.println(Arrays.toString(random));
}
}
Excuse me if any typos in the code.
Thanks,
Related 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.