简体   繁体   中英

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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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