英文:
Creating random integer array without duplicates
问题
public class Fill {
private static int size;
private static int maxVal;
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
// Ask user to enter the size of the array
System.out.print("Please enter the size of the array: ");
size = kb.nextInt();
// Ask user to enter the maximum value allowed
System.out.print("Please enter the maximum value: ");
maxVal = kb.nextInt();
// Call fill() method
int arr[] = fill(size, maxVal);
// Print filled array
System.out.print("[");
for (int i = 0; i < arr.length - 1; i++)
System.out.print(arr[i] + ",");
System.out.print(arr[arr.length - 1] + "]");
}
public static int[] fill(int size, int maxVal) {
int arr[] = new int[size];
Random random = new Random();
// Fills the array with random numbers between 0 and maximum value
if (size <= 0 || maxVal < size - 1) {
System.out.print("Incorrect Parameters. Please Retry");
main(null);
} else {
for (int j = 0; j < size; j++) {
arr[j] = random.nextInt(maxVal);
// Check array for duplicates
for (int k = j + 1; k < size; k++) {
if (arr[j] == arr[k]) {
//create new random array
fill(size, maxVal);
}
}
}
}
return arr;
}
}
英文:
I am trying to create a random array without duplicates.
The assignment is to take an integer array and maximum value from user, fills the array with random numbers between 0 and maximum value, and display random array without duplicates, WITHOUT using any other classes except random and scanner.
This is a sample output:
Please enter the size of the array: 10
Please enter the maximum value: 50
[39,2,17,49,12,19,40,31,42,15]
I need help in removing the duplicates. I am not sure if what I am doing is correct, I am a bit of a beginner but here is what I have so far. Help would be greatly appreciated. Thanks.
public class Fill {
private static int size;
private static int maxVal;
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
// Ask user to enter the size of the array
System.out.print("Please enter the size of the array: ");
size = kb.nextInt();
// Ask user to enter the maximum value allowed
System.out.print("Please enter the maximum value: ");
maxVal = kb.nextInt();
// Call fill() method
int arr[] = fill(size, maxVal);
// Print filled array
System.out.print("[");
for (int i = 0; i < arr.length - 1; i++)
System.out.print(arr[i] + ",");
System.out.print(arr[arr.length - 1] + "]");
}
public static int[] fill(int size, int maxVal) {
int arr[] = new int[size];
Random random = new Random();
// Fills the array with random numbers between 0 and maximum value
if (size <= 0 || maxVal < size - 1) {
System.out.print("Incorrect Parameters. Please Retry");
main(null);
} else {
for (int j = 0; j < size; j++) {
arr[j] = random.nextInt(maxVal);
// Check array for duplicates
for (int k = j + 1; k < size; k++) {
if(arr[j] == arr[k]) {
//create new random array
fill(size, maxVal);
}
}
}
}
return arr;
}
}
答案1
得分: 1
我已经编辑并修复了你的代码,如下所示:
public class Fill {
private static int size;
private static int maxVal;
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
// 要求用户输入数组的大小
System.out.print("请输入数组的大小:");
size = kb.nextInt();
// 要求用户输入允许的最大值
System.out.print("请输入最大值:");
maxVal = kb.nextInt();
// 调用 fill() 方法
int arr[] = fill(size, maxVal);
// 打印填充后的数组
System.out.print("[");
for (int i = 0; i < arr.length - 1; i++)
System.out.print(arr[i] + ",");
System.out.print(arr[arr.length - 1] + "]");
}
public static int[] fill(int size, int maxVal) {
int arr[] = new int[size];
Random random = new Random();
// 使用介于 0 和最大值之间的随机数填充数组
if (size <= 0 || maxVal < size) {
System.out.print("参数错误,请重试");
main(null);
} else {
for (int j = 0; j < size; j++) {
int newNumber = random.nextInt(maxVal + 1);
// 检查数组中是否已存在重复值
while (alreadyExist(newNumber, arr)) {
newNumber = random.nextInt(maxVal + 1);
}
arr[j] = newNumber;
}
}
return arr;
}
static boolean alreadyExist(int a, int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == a)
return true;
}
return false;
}
}
现在它不会返回重复的值了。
英文:
I have edited and fixed some issues in your code as below:
public class Fill {
private static int size;
private static int maxVal;
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
// Ask user to enter the size of the array
System.out.print("Please enter the size of the array: ");
size = kb.nextInt();
// Ask user to enter the maximum value allowed
System.out.print("Please enter the maximum value: ");
maxVal = kb.nextInt();
// Call fill() method
int arr[] = fill(size, maxVal);
// Print filled array
System.out.print("[");
for (int i = 0; i < arr.length - 1; i++)
System.out.print(arr[i] + ",");
System.out.print(arr[arr.length - 1] + "]");
}
public static int[] fill(int size, int maxVal) {
int arr[] = new int[size];
Random random = new Random();
// Fills the array with random numbers between 0 and maximum value
if (size <= 0 || maxVal < size ) {
System.out.print("Incorrect Parameters. Please Retry");
main(null);
} else {
for (int j = 0; j < size; j++) {
int newNumber = random.nextInt(maxVal + 1);
// Check array for duplicates
while(alreadyExist(newNumber, arr)){
newNumber = random.nextInt(maxVal + 1);
}
arr[j] = newNumber;
}
}
return arr;
}
static boolean alreadyExist(int a, int[] arr){
for(int i = 0 ; i < arr.length ; i++){
if(arr[i] == a) return true;
}
return false;
}
}
Now it does not return any repetitive value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论