1

I'm just starting out in programming. I am trying to develop a program that collects any amount of integer numbers and reverses the order in which they were entered. Say 1 6 8 9 4 9 becomes 9 4 9 8 6 1 This is what I did:

    System.out.println("Enter ten  numbers:");
    int[] n = new int[10];
    for (int i =0; i<n.length; i++)
    n[i] = input.nextInt();

 for(int i =0; i<n.length-1; i++)
     for (int j= n.length-1; j>0; j--){
    int temp = n[i];
    n[i] = n[j];
    n[j] = ;
  }
}

2 Answers2

1

If you want to swap-reverse all numbers you can do this:

for(int i = 0; i < array.length / 2; i++)
{
    int temp = array[i];
    array[i] = array[array.length - i - 1];
    array[array.length - i - 1] = temp;
}
Vollmilchbb
  • 481
  • 6
  • 20
0

If all you want to do is print them out in the reverse order, why reverse the array? Just print them:

   System.out.println("Enter ten  numbers:");
    int[] n = new int[10];
    for (int i =0; i<n.length; i++) {
       n[i] = input.nextInt();
    }
    for ( int i=n.length-1; i >=  0; i-- ) {
       System.out.println( n[i] );
    }
FredK
  • 4,094
  • 1
  • 9
  • 11
  • You are right on the printing part but is it possible to collect the numbers going into the println statement back into the original array? If so will that reverse the original array? – Malvins Chelsi Nov 17 '15 at 21:25