Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Takes user input, to create a 10 element array.
- * Displays the highest, and lowest values in the array.
- * Creates a reversed copy of the original array, and displays it for the user.
- */
- package arrayreversal;
- import java.util.Arrays;
- import java.util.Scanner;
- public class ArrayReversal {
- public static void main(String[] args) {
- //sets permanent value for the number of elements
- final int NUM_ELEMENTS = 10;
- //initializes the variables
- int[] numbers = new int[NUM_ELEMENTS];
- int[] backWards = new int[numbers.length];
- //Creates a new scanner object
- Scanner in = new Scanner(System.in);
- //prompts the user for their data
- System.out.println("Please enter 10 whole numbers: ");
- //loops through and collects each element, then assigns to the array
- for (int index = 0; index < numbers.length; index ++)
- {
- System.out.print("Number " + (index + 1) + ": " );
- numbers[index] = in.nextInt();
- }
- //displays the array values for the user
- System.out.print("\nThe numbers you entered are: ");
- for (int index = 0; index < NUM_ELEMENTS; index ++)
- System.out.print(" " + numbers[index] + " ");
- System.out.print("\n");
- int maxValue = numbers[0];
- int minValue = numbers[0];
- //loops through the array, checking for the lowest value
- for (int index = 0; index < numbers.length; index++)
- if (minValue > numbers[index]){
- minValue = numbers[index];
- }
- //displays the lowest value from the array
- System.out.println("Your lowest value was " + minValue);
- //loops through the array, checking for the highest value
- for (int index = 0; index < numbers.length; index++)
- if (numbers[index] >= maxValue) {
- maxValue = numbers[index];
- }
- //displays the highest value from the array
- System.out.println("Your highest value was " + maxValue);
- //copies the values of the numbers array,
- //then populates backWards as a reversed array
- for (int index = 0; index < numbers.length; index ++){
- backWards[index] = numbers[numbers.length - 1 - index];
- if (backWards.length == NUM_ELEMENTS);
- }
- //displays the contents of backWards for the user
- System.out.println("Your numbers in reverse order are: "
- + Arrays.toString(backWards));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement