你好!我是Java的初学者,关于`Arrays`我有一个问题。

huangapple go评论73阅读模式
英文:

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而其余的数字都是不同的就像这样[24103900750]

我做错了什么我是否走对了方法
任何帮助将不胜感激

    int[] myArray = {12345678910};
    int[] myArray2 = {12345678910};
    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 &lt; myArray2.length - 1; i++) {
int ranIndex = (int) (Math.random() * 10 - 1) + 1;
if (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 9);
}
if (myArray2[ranIndex] &gt; 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 你好!我是Java的初学者,关于`Arrays`我有一个问题。

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 &lt; myArray2.length ; i++) {
int ranIndex = (int) (Math.random() * 10 - 1) + 1;
while (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 10);
}
if (myArray2[ranIndex] &gt; 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));
}
}

Problems were :

  • i &lt; length is enough, i &lt; length - 1 discard the last element
  • Math.random() is included &gt;=0 and &lt;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 by while (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 the if by while allows the code to try until it founds a new not used item !

huangapple
  • 本文由 发表于 2020年10月1日 23:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64158226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定