
Kathryn C. answered 12/07/21
Computer Science Graduate and Experienced Tutor/Instructor
Hi Harris!
Programming reflects math quite often. Just like in math, the median of three numbers is the second number when the numbers are sorted.
There are various ways of representing this in code that really tests your problem-solving skills.
Some hints/steps I can give you are:
- Have some way of storing the three numbers. What are their data types? int/long, float/double, or char/string? Two ways of storing these numbers are 3 separate variables or an array/data structure of some kind.
- Determine how you can sort/access these three numbers. If it is an array, you will need to sort the array. If you have three variables you will need to determine which one is greater than one of the other variables but less than the leftover variable.
- Perform input/output. Remember the steps for performing input:
- Import the library for Scanner:
- Make Scanner object variable and indicate the kind of input you will take. (example below uses Console input.
- Prompt the user with output for the kind of input you want.
- Use the appropriate method for taking in the kind of input you want. Refer to the official docs for Scanner at https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html to see what methods are available to you.
Example: