So I’m working on an OOP program that is meant to create 50 unique numbers, by using a random number generator that ensures no repeating numbers. I have the random part down and I’m using an extra method which swaps the numbers but I don’t know to only swap number if they are already used, I hope this is understandable.
import java.util.Random;
public class Project_1
{
public static void main(String[] args)
{
Random rand = new Random();
int a[][] = new int[11][6];
for (int i = 1; i <= 10; i++) //row of values which fill in Student number
{
System.out.print("Student " + i + ":");
System.out.print("\t");
for (int j = 1; j <= 5; j++) //j is limited up to 5 columns
{
a[i][j] = 1 + rand.nextInt(50);
CheckNumbers(a[i][j]); //input of the checkNumbers method
System.out.print(a[i][j] + "\t"); // meaning that the numbers fill out according to table coordinates
}
System.out.println();
}
}
public static void CheckNumbers(int[] x, int[] y)
{
int temp;
for (int j = 0; j < 50; j++) //j must start at 1??? and we have 50 iterations, so countercontrolled to 50?
{
temp = x[i]; //this can be used to swap the numbers
x[i] = y[i];
y[i] = temp;
}
}
}
This is my program and if ran without the input of the CheckNumbers method my answer was
----jGRASP exec: java Project_1
Student 1: 8 35 8 5 40
Student 2: 46 3 44 40 8
Student 3: 27 47 28 11 3
Student 4: 30 25 43 8 34
Student 5: 3 12 45 6 5
Student 6: 19 37 33 14 14
Student 7: 9 31 6 39 32
Student 8: 16 6 23 28 31
Student 9: 19 34 49 42 11
Student 10: 26 3 17 16 15
----jGRASP: operation complete.
However as you see 8 is repeated (among may other numbers) so I put in the CheckNumbers method to stop it but I have an error filled compile message… so I am assuming that I have many errors in my CheckNumbers, so I need major help with that too.
Thank you all for your help!!!!
I’m not going to correct your methods. But if you want to create a random without repeating, create your own custom class.
Usage:
EDIT: JavaDoc added!!