Question: Modify your PromptNumbers program from the previous exercise so that it uses a static method to print the elements in forward order and another static
Modify your PromptNumbers program from the previous exercise so that it uses a static method to print the elements in forward order and another static method to print the array elements in backward order. The program's behavior should be the same as before.
How many numbers will you enter? 4 Type a number: 12 Type a number: 8 Type a number: -2 Type a number: 39 Your numbers in forward order: 12 8 -2 39 Your numbers in backward order: 39 -2 8 12
public class PromptNumbers2 { public static void main(String[] args) { int count = console.nextInt(); int[] nums = new int[count]; System.out.println("Your numbers in forward order:"); printForward(nums);
System.out.println("Your numbers in backward order:"); printBackward(nums); }
// Prints the elements of the given array in forward order. public static void printForward(int[] a) { for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } // Prints the elements of the given array in backward order. public static void printBackward(int[] a) { for (int i = a.length - 1; i >= 0; i--) { System.out.println(a[i]); }}}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
