Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Write a program named Median.java that asks the user for three integers and outputs t

    Write a program named Median.java that asks the user for three integers and outputs the median.
    I have to do this using if else statements. It seems really easy, but I can't figure it out. Help!

    Here's my code:
    Code:
    import java.util.*;
    
    public class Median {
    
    	public static void main(String[] args) {
    	
    		Scanner console = new Scanner(System.in);
    		
    		System.out.println("Please enter number 1");
    		int max = console.nextInt();
    		int min = max;
    		int median = min;
    		
    		for (int i=2; i <= 3; i++) {
    			System.out.println("Please enter number "+i);
    			int enteredNumber = console.nextInt();
    			if (enteredNumber > max) {
    				max = enteredNumber;
    			}
    			else {
    				enteredNumber = median;
    			}
    			if (enteredNumber < min) {
    				min = enteredNumber;
    			}
    			else {
    				enteredNumber = median;
    			}
    		}
    		System.out.println("The median is: "+median);
    	}
    }
    When I run this it just gives me back the first number I entered.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,324 Times in 4,290 Posts
    This is the JAVASCRIPT forum. Your program is written in JAVA.

    About the only thing the two languages have in common is the first 4 letters of their names.

    But having said that... Yes? Did you pretend that YOU are the computer and run your program, using your brain instead of the program? If you did, you would see that indeed you *WILL* always get the first number entered as the median.

    How many places in your code do you *CHANGE* the value of the variable named median? HINT: ONE.

    Having said that, even if you fixed the obvious mistake, your code will in no way reliably get the median number.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Oops, I didn't see that it was Javascript lol. Sorry.
    But, yes I did see that, and I tried changing it in every way I could think of. I'm out of ideas, so how could I get this to work.
    Last edited by InsTanCeZ; 02-08-2012 at 06:11 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •