Directory
1 Problem Description
2 Solutions
1 problem description Problem DescriptionWrite a program that reads a set of integers (no more than 20) and stores them in an integer array. When the user enters 0 o'clock, the input ends. The program then stores the values in the array in reverse order and prints them out. For example: Suppose the user enters a set of data: 7 19-5 6 2 0, then the program will save the first five valid data in an array, that is, 7 19-5 6 2, and then put the values in this array back in reverse order, which becomes 2 6-5 19 7, and then print them out.
input Format: Enter only one row, consisting of several integers, separated by a space, the end of the integer is 0.
output format: The output is only one line, that is, the integer after the reverse order, separated by a space, there is no space at the end.
input/Output sampleSample Input7 19-5 6 2 0Sample Output2 6-5 7
2 Solutions
The specific code is as follows:
Importjava.util.ArrayList;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {ArrayList<Integer> list =NewArraylist<integer>(); Scanner in=NewScanner (system.in); while(true) { intA =In.nextint (); if(A = = 0) Break; List.add (a); } for(inti = List.size ()-1;i >= 0;i--) System.out.print (List.get (i)+" "); }}
Algorithm Note _158: Algorithm improves reverse order (Java)