Java – need help writing numbers in reverse order
•
Java
I got help with the task I don't ask anyone to do my job, but I really honestly insist on how to do it
I should write a program that prompts the user to enter 10 numbers, and then let it write all the numbers in reverse order
Example: enter 10 numbers: 23 89 21 55 67 99 13 98 78 reverse order: 78 98 13 99 89 67 55 21 89 23
So far, all I have is how to get user input If someone can push me in the right direction, I will be very grateful!
import java.util.*; public class ReverseNumbers { public static void main(String[] args) { Scanner keyboard = new Scanner(system.in); int[] values; values = new int[10]; //Ask the user to enter 10 integers System.out.println("Please enter 10 numbers:"); for (int i = 0; i< values.length; i++) { values[i] = keyboard.nextInt(); } int[] reverseNums; reverseNums = new int[10]; for (int i = (values.length -1); i>= 0; i--) { reverseNums[ reverseNums.length -1 -i ] = values[ i ]; System.out.println( reverseNums[ i ] ); } } }
Solution
If you do not want to store inverted values
for (int i = (values.length -1); i>= 0; i--) { System.out.println( values[ i ] ); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码