编写一个java数组并获得太多输出

时间:2016-05-09 16:09:15

标签: java arrays

// declare an array (named numbs) that holds 10 ints
int[] numbs = new int[10];
// write a for loop that loops 10 times and generates a random number between 0 and 100
for (int i = 0; i < numbs.length; i++) {
    numbs[i] = (int) (Math.random() * 100 + 1);
}
// print the values
for (int number : numbs) {
    System.out.println(number);

    // write a for loop that displays to the screen each random number in the array named numbs
    for (int i = 0; i < numbs.length; i++) {
        System.out.println("the random number for element " + i + " is " + numbs[i]);
    }
}

我希望我的最终输出只显示十个数字,但我得到的是重复10次的10个数字,而不是 元素1的随机数是1 元素2的随机数是2 随机数......等十个

我明白了 元素1的随机数是1 元素2的随机数是2 随机数......等十 然后它回到最顶层的数字和打印 “元素1的随机数再次为1” 然后“元素2的随机数再次为2”

我做错了什么?

4 个答案:

答案 0 :(得分:0)

您正在使用嵌套的for循环。你只需要你的第三个循环。

public class FArray {
  public static void main(String[] args) {
  int[] numbs = new int[10];
  for (int i = 0; i < numbs.length; i++) {
    numbs[i] = (int) (Math.random() * 100 + 1);
  }
  // print the values
  for (int i=0; i<numbs.length; i++) {
    System.out.println("the random number for element "+ i +" is "+numbs[i]);
  }
 }
}

答案 1 :(得分:0)

您将数据打印10次X 10次因为您有嵌套循环通知可能您想要这样的事情

    public static void main(String[] args) {
    // declare an array (named numbs) that holds 10 ints
    int[] numbs = new int[10];
    // write a for loop that loops 10 times and generates a random number
    // between 0 and 100
    for (int i = 0; i < numbs.length; i++) {
        numbs[i] = (int) (Math.random() * 100 + 1);
    }

    // write a for loop that displays to the screen each random number in
    // the array named numbs
    for (int i = 0; i < numbs.length; i++) {
        System.out.println("the random number for element " + i + " is " + numbs[i]);
    }
}

}

答案 2 :(得分:0)

  

我希望我的最终输出只显示十个数字,但我得到的是重复10次的10个数字而不是元素的随机数   1是1,元素2的随机数是2,随机数等等   十点

你正在循环 2 次,一个for循环可以看到数组中的所有随机元素...删除其中一个循环(例如删除增强的for循环)到只看到你生成的10个数字..

实施例

    public static void main(String[] args) throws ParseException {
        // declare an array (named numbs) that holds 10 ints
        int[] numbs = new int[10];
        // write a for loop that loops 10 times and generates a random number between 0 and 100
        for (int i = 0; i < numbs.length; i++) {
            numbs[i] = (int) (Math.random() * 100 + 1);
        }
        // print the values

        // write a for loop that displays to the screen each random number in the array named numbs
        for (int i = 0; i < numbs.length; i++) {
            System.out.println("the random number for element " + i + " is " + numbs[i]);
        }
}

答案 3 :(得分:0)

代码的最后部分:

for (int number : numbs) {
    System.out.println(number);
// write a for loop that displays to the screen each random number in the array named numbs
for (int i = 0; i < numbs.length; i++) {
    System.out.println("the random number for element " + i + " is " + numbs[i]);
}

您告诉它要执行以下操作: foreach int存储在数组numbs中,打印那个int,然后,foreach元素numbs [i]用i = 0..numbs.length-1打印那句话。

因此,您的数组中有10个元素。对于每个元素,您再次检查阵列的所有位置。这就是你获得10 * 10结果的原因。

编写以下内容就足够了:

 for (int i = 0; i < numbs.length; i++) {
    System.out.println("the random number for element " + i + " is " + numbs[i]);
  }