Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2012, 04:40 AM   PM User | #1
InsTanCeZ
New to the CF scene

 
Join Date: Jan 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
InsTanCeZ is an unknown quantity at this point
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.
InsTanCeZ is offline   Reply With Quote
Old 02-08-2012, 05:57 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,193
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-08-2012, 06:05 AM   PM User | #3
InsTanCeZ
New to the CF scene

 
Join Date: Jan 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
InsTanCeZ is an unknown quantity at this point
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..
InsTanCeZ is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:38 PM.


Advertisement
Log in to turn off these ads.