Advertisement
tolfasn

Array_Reversal

Oct 24th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. /*
  2.  * Takes user input, to create a 10 element array.
  3.  * Displays the highest, and lowest values in the array.
  4.  * Creates a reversed copy of the original array, and displays it for the user.
  5.  */
  6. package arrayreversal;
  7. import java.util.Arrays;
  8. import java.util.Scanner;
  9.  
  10. public class ArrayReversal {
  11.  
  12. public static void main(String[] args) {
  13.  
  14.     //sets permanent value for the number of elements
  15.     final int NUM_ELEMENTS = 10;
  16.  
  17.     //initializes the variables
  18.     int[] numbers = new int[NUM_ELEMENTS];
  19.     int[] backWards = new int[numbers.length];
  20.  
  21.     //Creates a new scanner object
  22.     Scanner in = new Scanner(System.in);
  23.  
  24.     //prompts the user for their data
  25.     System.out.println("Please enter 10 whole numbers: ");
  26.  
  27.     //loops through and collects each element, then assigns to the array
  28.     for (int index = 0; index < numbers.length; index ++)
  29.     {
  30.         System.out.print("Number " + (index + 1) + ": " );
  31.             numbers[index] = in.nextInt();
  32.     }
  33.  
  34.     //displays the array values for the user
  35.     System.out.print("\nThe numbers you entered are: ");
  36.  
  37.     for (int index = 0; index < NUM_ELEMENTS; index ++)
  38.         System.out.print(" " + numbers[index] + " ");
  39.             System.out.print("\n");
  40.            
  41.     int maxValue = numbers[0];
  42.     int minValue = numbers[0];
  43.  
  44.     //loops through the array, checking for the lowest value
  45.     for (int index = 0; index < numbers.length; index++)
  46.         if (minValue > numbers[index]){
  47.             minValue = numbers[index];
  48.         }
  49.     //displays the lowest value from the array
  50.     System.out.println("Your lowest value was " + minValue);
  51.  
  52.     //loops through the array, checking for the highest value        
  53.     for (int index = 0; index < numbers.length; index++)
  54.         if (numbers[index] >= maxValue) {
  55.             maxValue = numbers[index];
  56.         }
  57.     //displays the highest value from the array
  58.     System.out.println("Your highest value was " + maxValue);
  59.    
  60.     //copies the values of the numbers array,
  61.     //then populates backWards as a reversed array
  62.     for (int index = 0; index < numbers.length; index ++){
  63.         backWards[index] = numbers[numbers.length - 1 - index];
  64.         if (backWards.length == NUM_ELEMENTS);
  65.         }
  66.    
  67.     //displays the contents of backWards for the user
  68.     System.out.println("Your numbers in reverse order are: "  
  69.                     + Arrays.toString(backWards));
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement