Advertisement
Adumb_Copper

Array Medians

Nov 24th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. public class ArrayMedians {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         //same old
  9.         int tara;
  10.         double amber;
  11.         Scanner get = new Scanner(System.in);
  12.        
  13.         //asks the user how long the array should be
  14.         System.out.println("Hey what's up? You want the median of a list of numbers? Cool, cool. I can do that. How many numbers you want?");
  15.         tara = get.nextInt();
  16.        
  17.         //asks the user to enter numbers into the array
  18.         System.out.println("A'ight, a'ight. " + tara + " numbers? I can do that. You just tell me what the numbers are and we gon' get it done.");
  19.        
  20.         int [] arrayRice = new int[tara];
  21.        
  22.         for (int loop = 0; loop < tara; loop++) {
  23.             arrayRice[loop] = get.nextInt();   
  24.         }
  25.        
  26.        
  27.        
  28.  
  29.         Arrays.sort(arrayRice);
  30.        
  31.        
  32.         //calculates and prints median of numbers entered
  33.        
  34.         if (tara % 2 == 0)  
  35.             amber = (double) (arrayRice[tara / 2] + arrayRice[tara / 2 - 1]) / 2;
  36.         else amber = arrayRice[tara / 2];
  37.        
  38.         System.out.println("Ay I figured it out. The median is " + amber +", dawg.");
  39.  
  40.        
  41.        
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement