0

In an exercice at class, we need to ask to the user (in these example) ten numbers and then the program just reverse it. Already check it and the way to do it it's with a for statement.

for (int i = 0 ; i < n ; i ++) {
        System.out.println("In the position "+i+" now we have these one "+nkb[in-1-i]);
    }

Ok these is a solution and it's ok, but my question is, can be possible in other way than with another for statement?

Here it's the full code of the exercice.

package *;
import java.util.Scanner;
public class * {
    // n = limit of inputs 
    private static final int n = 10;
    static Scanner kb = new Scanner(System.in);
    public static void main(String[] args) {
      //nkb = number from keyboard
      int [] nkb = new int[n];
      // in = number of length
      int in = nkb.length;
      //for input user
      for (int i = 0; i < n; i ++){
          System.out.println("Writte a number: ");
          nkb[i] = kb.nextInt();
      }//end for
      for (int i = 0; i < n; i ++){
          System.out.println("In these position "+i+" now we have these number: "+nkb[in-1-i]);
      }//end for reverse        
    }//end method
}//end class 
mcGuffin
  • 69
  • 1
  • 11
  • 1
    Use a `List` and call `Collections.reverse()`. – shmosel Nov 13 '17 at 23:24
  • 1
    Of course you can do it without a for loop, for example a while loop. Or recursion. Or you could put them into the array in reverse order and then not need to do anything else. – Andy Turner Nov 13 '17 at 23:24

0 Answers0