Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2008, 05:56 PM
Member
 
Join Date: Dec 2007
Posts: 2
gammaman is on a distinguished road
Problem comparing three numbers
For some reason when I compare the following and look to find the largest and smallest of three numbers, it results with the largest and smallest being the same number, which is the large number, Please help


//finding the largest and smallest numbers
if ((num1>num2) && (num1>num3));
{
large = num1;
}
else
if ((num2>num1) && (num2>num3));
{
large = num2;
}
else
if ((num3>num1) && (num3>num2));
{
large = num3;
}
else
if ((num1<num2) && (num1<num3));
{
small = num1;
}
else
if ((num2<num1) && (num2<num3));
{
small = num2;
}
else
if ((num3<num1) && (num3<num2));
{
small = num3;
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-09-2008, 07:43 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
There seem to be two problems with the above code. Firstly it won't compile so I am surprised you got any result., if conditions should not end with a ;, and else clauses need to be in { } pairs, i.e.

Code:
if ((num2<num1) && (num2<num3)); { small = num2; } else if ((num3<num1) && (num3<num2)); { small = num3; }
needs to be rewritten (using your formatting)

Code:
if ((num2<num1) && (num2<num3)) { small = num2; } else { if ((num3<num1) && (num3<num2)) { small = num3; } }
Secondly, even if you tidy up the syntax you find that the setting of the smallest number never happens because it always an else clause that never gets triggered. I suspect you wanted to find the largest and then the smallest number separately, try the following very quick and dirty class based on your algorithm

Code:
public class Test42 { public static void main (String[] args){ testNumbers(); } public static void testNumbers(){ int num1 = 31, num2 = 76, num3 =54; int small = -1, large = -1; // calculate largest if((num1 > num2)&&( num1 > num3)){ large = num1; } else { if((num2>num1)&&(num2>num3)){ large = num2; } else { if((num3>num1)&&(num3>num2)){ large = num3; } } } //calculate smallest if((num1<num2)&&(num1<num3)){ small = num1; } else { if((num2<num1)&&(num2<num3)){ small = num2; } else { if((num3<num1)&&(num3<num2)){ small = num3; } } } // print results System.out.println(large +"**"+small); } }
Have a look through it and see the differences and how it works. Also something to think about, your current algorithm will not work if two numbers are the same and either the largest or the smallest.
__________________
-- Hope that helps

Last edited by jelly : 02-09-2008 at 07:46 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing Images shaungoater Advanced Java 0 03-17-2008 12:38 PM
Comparing null problem thirdy_veritech New To Java 2 02-06-2008 10:46 AM
Comparing problem mcal New To Java 1 01-24-2008 05:56 AM
comparing Feng New To Java 2 11-23-2007 11:40 AM
Comparing JavaWebFrameworks pegitha Web Frameworks 1 05-18-2007 08:23 PM


All times are GMT +3. The time now is 04:40 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org