Reputation: 65
The following code needs to print the random numbers in the array in order and reverse order. I have the reverse order printing but for some reason I can't seem to get it to print in original order. I'm not sure what to modify. What I currently have prints out a blank line for the "in order" and the numbers for "reverse order". What do I need to fix? Thank you!
public class RandomPrintOut
{
//-----------------------------------------------------------------
//
//-----------------------------------------------------------------
public static void main (String[] args)
{
int numbers[] = new int [10];
for (int i = 0 ; i < 10 ; i++)
{
numbers [i] = (int) (Math.random () * 100);
}
System.out.println ("The size of the array: " + numbers.length);
System.out.println ("The numbers in order:");
for (int index = numbers.length+1; index <= 10; index++)
System.out.print (numbers[index] + " ");
System.out.println ("\nThe numbers in reverse order:");
for (int index = numbers.length-1; index >= 0; index--)
System.out.print (numbers[index] + " ");
}
}
Upvotes: 0
Views: 13402
Reputation: 3197
For the code below:
for (int index = numbers.length+1; index <= 10; index++)
Here the initialized value for index
is number.length+1, this value is 10+1=11
. This is greater than 10, as a result, the codition is never satisfied in this for-loop. That's why the array is not printed in order.
You need to do the change as follows:
Frome
for (int index = numbers.length+1; index <= 10; index++)
System.out.print (numbers[index] + " ");
to
for (int index = 0; index <10; index++)
System.out.print(numbers[index] + " ");
Code after change is as follows:
public class RandomPrintOut { //----------------------------------------------------------------- // //----------------------------------------------------------------- public static void main (String[] args) {
int numbers[] = new int [10];
for (int i = 0 ; i < 10 ; i++)
{
numbers [i] = (int) (Math.random () * 100);
}
System.out.println ("The size of the array: " + numbers.length);
System.out.println ("The numbers in order:");
for (int index = 0; index < 10; index++)
System.out.print (numbers[index] + " ");
System.out.println ("\nThe numbers in reverse order:");
for (int index = numbers.length-1; index >= 0; index--)
System.out.print (numbers[index] + " ");
} }
An result output printed in Console:
The size of the array: 10
The numbers in order:
31 50 49 99 29 54 41 16 7 21
The numbers in reverse order:
21 7 16 41 54 29 99 49 50 31
Upvotes: 3
Reputation: 1297
public class RandomPrintOut {
//-----------------------------------------------------------------
//
//-----------------------------------------------------------------
public static void main (String[] args) {
int numbers[] = new int [10];
for (int i = 0 ; i < 10 ; i++) {
numbers [i] = (int) (Math.random () * 100);
}
System.out.println ("The size of the array: " + numbers.length);
System.out.println ("The numbers in order:");
for (int index = 0; index <= numbers.length-1; index++) {
System.out.print (numbers[index] + " ");
}
System.out.println ("\nThe numbers in reverse order:");
for (int index = numbers.length-1; index >= 0; index--) {
System.out.print (numbers[index] + " ");
}
}
}
Upvotes: 0
Reputation: 2785
Take a closer look at the values of index
over this loop
for (int index = numbers.length+1; index <= 10; index++)
System.out.print (numbers[index] + " ");
Try doing it by hand and see what it looks like. That should help you identify the problem.
Upvotes: 0