Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- public class ArrayMedians {
- public static void main(String[] args) {
- //same old
- int tara;
- double amber;
- Scanner get = new Scanner(System.in);
- //asks the user how long the array should be
- 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?");
- tara = get.nextInt();
- //asks the user to enter numbers into the array
- 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.");
- int [] arrayRice = new int[tara];
- for (int loop = 0; loop < tara; loop++) {
- arrayRice[loop] = get.nextInt();
- }
- Arrays.sort(arrayRice);
- //calculates and prints median of numbers entered
- if (tara % 2 == 0)
- amber = (double) (arrayRice[tara / 2] + arrayRice[tara / 2 - 1]) / 2;
- else amber = arrayRice[tara / 2];
- System.out.println("Ay I figured it out. The median is " + amber +", dawg.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement