英文:
Hi! I'm a beginner in Java and I have a question regarding `Arrays`
问题
我是Java的初学者,关于`Arrays`我有一个问题。我一直在解决这个任务,任务要求:编写一个程序,生成1到10的十个随机排列。要生成随机排列,你需要填充一个数组,数组中包含从1到10的数字,确保数组中没有两个相同的数字。创建第二个数组,并填充数字1到10。重复进行10次,从第二个数组中随机选择一个元素,将其从数组中移除,并附加到排列数组中。在解决这个任务时,不允许使用ArrayList。
目前为止,我已经做了以下工作,但是我的问题是,它会输出一些0,而其余的数字都是不同的,就像这样:[2,4,10,3,9,0,0,7,5,0]。
我做错了什么?我是否走对了方法?
任何帮助将不胜感激!
int[] myArray = {1,2,3,4,5,6,7,8,9,10};
int[] myArray2 = {1,2,3,4,5,6,7,8,9,10};
int[] permutation = new int[10];
for (int i = 0; i < myArray2.length - 1; i++) {
int ranIndex = (int) (Math.random() * 10 - 1) + 1;
if (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 9);
}
if (myArray2[ranIndex] > 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));
英文:
I'm a beginner in Java and I have a question regarding Arrays
. I've been struggling with this task where it says: Write a program that produces ten random permutations of the numbers 1 to 10. To
generate a random permutation, you need to fill an array with the numbers 1 to 10
so that no two entries of the array have the same contents. Make a second array and fill it with the numbers 1 to 10. Repeat 10 times, Pick a random element from the second array. Remove it and append it to the permutation array. I am not allowed to use Arraylist to solve this task.
This is what I have done so far and my issue right now is that it prints out some zeroes while the rest of the numbers are all different like it should be: [2, 4, 10, 3, 9, 0, 0, 7, 5, 0].
What am I doing wrong, Am I even on the right way?
Any help would be appreciated!
int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] permutation = new int[10];
for (int i = 0; i < myArray2.length - 1; i++) {
int ranIndex = (int) (Math.random() * 10 - 1) + 1;
if (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 9);
}
if (myArray2[ranIndex] > 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));
答案1
得分: 1
import java.util.Arrays;
public class Permutation {
public static void main(String[] args) {
int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] permutation = new int[10];
for (int i = 0; i < myArray2.length; i++) {
int ranIndex = (int) (Math.random() * 10);
while (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 10);
}
if (myArray2[ranIndex] > 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));
}
}
英文:
You were close
import java.util.Arrays;
public class Permutation {
public static void main(String[] args) {
int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] permutation = new int[10];
for (int i = 0; i < myArray2.length ; i++) {
int ranIndex = (int) (Math.random() * 10 - 1) + 1;
while (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 10);
}
if (myArray2[ranIndex] > 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));
}
}
Problems were :
i < length
is enough,i < length - 1
discard the last elementMath.random()
is included>=0
and<1
if you multiply it by 9, you can't get the last element of myArray2 and thus have an infinite loop.if (myArray2[ranIndex] == 0) {
should be replaced bywhile (myArray2[ranIndex] == 0) {
. It was your main issue : in case the second attempt failed to find a new number in myArray2, it would leave 0 there. Replacing theif
bywhile
allows the code to try until it founds a new not used item !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论