简体   繁体   English

为什么我的程序不输出数组元素?

[英]Why is my program not outputting my array elements?

What I am trying to do is output each element of my array to the system. 我想做的是将数组的每个元素输出到系统。 Below is how the array elements were entered and my issue is beneath that. 下面是输入数组元素的方式,而我的问题就在此之下。 Why is it not outputting the array elements? 为什么不输出数组元素?

for (int i = 0; i <arrayLength; i++) {
    double array[] = new double[arrayLength];
    array[i] = IO.readInt("Enter number: " + (i+1));
    count++;
} 
for (int i = 0; i <arrayLength; i++) {
    System.out.println(array[i]); 
}

It's because you create a new array every time in the first loop. 这是因为您在第一个循环中每次都会创建一个新数组。 You have to declare the array before the loop. 您必须在循环之前声明数组。

double array[] = new double[arrayLength];

for (int i = 0; i <arrayLength; i++)
{
    array[i] = IO.readInt("Enter number: " + (i+1));
} 
for (int i = 0; i <arrayLength; i++)
{
    System.out.println(array[i]); 
}

You are creating a new array on each iteration so please keep the array declaration outside the loops. 您将在每次迭代中创建一个新数组,因此请在循环之外保留数组声明。

double array[] = new double[arrayLength];
for (int i = 0; i <arrayLength; i++)
{
    array[i] = IO.readInt("Enter number: " + (i+1));
    System.out.println(array[i]); //reference the value saved in variable out of the for scope.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM