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 Newbie help with if/else statments

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-09-2012, 02:41 AM PM User | #1 cpole New to the CF scene   Join Date: Jan 2012 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Newbie help with if/else statments I am stuck on a homework problem. I think I am on the right track but am getting stuck on the if/else statement. I could be totally wrong though. Here is my assignment: Write a program that reads three integer inputs into variables. Display the input values in both the ordered entered and in sorted order. This program should sort the numbers so that value1 <= value2 <= value3. I have only been learning Java for a couple weeks, any help would be appreciated. Hoping to make this a career one day. Here is the code: import java.util.Scanner; public class OrderVariable { public static void main (String [] args) { Scanner input = new Scanner (System.in); System.out.prntln("Enter first variable"); int VariableOne = input.nextInt(); System.out.prntln("Enter second variable"); int VariableTwo = input.nextInt(); System.out.prntln("Enter third variable"); int VariableThree = input.nextInt(); if (VariableOne > VariableTwo && VariableOne > VariableThree) int Biggest = VariableOne; else if (VariableTwo > VariableOne && VariableTwo > VariableThree) int Biggest = VariableTwo; else (VariableThree > VariableOne && VariableThree > VariableTwo) int Biggest = VariableThree; if (VariableOne < VariableTwo && VariableOne < VariableThree) int Smallest = VariableOne; else if (VariableTwo < VariableOne && VariableTwo < VariableThree) int Smallest = VariableTwo; else (VariableThree < VariableOne && VariableThree < VariableTwo) int Smallest = VariableThree; if (VariableOne < Biggest && VariableOne > Smallest){ int Mid = VariableOne; else if (VariableTwo < Biggest && VariableTwo > Smallest) int Mid = VariableTwo; else (VariableThree < Biggest && VariableThree > Smallest) int Mid = VariableThree; System.out.println(VariableOne + ", " + VariableTwo + ", " + VariableThree); System.out.println(Max + ", " + Mid + ", " + Min); } }
 01-09-2012, 02:50 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,597 Thanks: 4 Thanked 2,597 Times in 2,566 Posts Have you learned the use of arrays? If so, this is far more trivial than this: PHP Code: ``` int[] iArray = new int[3]; iArray[0] = input.nextInt(); iArray[1] = input.nextInt(); iArray[2] = input.nextInt(); System.out.println(iArray[0] + ", " + iArray[1] + ", " + iArray[2]); Arrays.sort(iArray); System.out.println(iArray[0] + ", " + iArray[1] + ", " + iArray[2]);  ``` Otherwise, if/elseif's would work fine. You have two problems above; the first is that else's cannot carry conditions. The second is that you have an opening brace where it doesn't belong. IMO you should brace every branch even if its only 1 instruction. And finally, your accessing variables that don't exist. You used Biggest and Smallest, not Max and Min. Another approach would be to use a more simplified drill: PHP Code: ``` if (v1 >= v2) {     if (v1 >= v3)     {         // first is larger than both the second and the third         biggest = v1;         if (v2 >= v3)         {             // third is smaller then the second, so therefore third is the smallest             smallest = v3;             mid = v2;         }         else         {             smallest = v2;             mid = v3;         }     }     else     {         biggest = v3;         smallest = v2;         mid = v1;     } } else if (v2 >= v3) {     // With the first if, we know that v2 now must be the biggest     biggest = v2;     if (v1 >= v3)     {         smallest = v3;         mid = v1;     }     else     {         smallest = v1;         mid = v3;     } } else {     // Since neither v1 or v2 is the largest, we know that v3 is the largest:     biggest = v3;     if (v1 >= v2)     {         smallest = v2;         mid = v1;     }     else     {         smallest = v1;         mid = v2;     } }  ``` Hmm, yep that looks like it would work too.

 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 08:27 PM.