Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Help, I'm Confused

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 02-08-2012, 07:07 AM PM User | #1 InsTanCeZ New to the CF scene   Join Date: Jan 2012 Posts: 5 Thanks: 1 Thanked 0 Times in 0 Posts Help, I'm Confused 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.
 02-08-2012, 02:43 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,584 Thanks: 4 Thanked 2,596 Times in 2,565 Posts The logic is half right, but you are overwriting the enteredNumber with that of the median which is backwards. Its easier than this though, with only 3 numbers: PHP Code: ``` if (enteredNumber > max){    median = max;    max = enteredNumber;}else if (enteredNumber < min){    median = min;    min = enteredNumber;}else{    median = enteredNumber;}  ``` If you can't use if/elseif, then the logic of if/elseif/else is identical to if/else(if/else). Also, in the future please select a title that better describes the issue you are having.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:43 PM.