Results 1 to 5 of 5
 10162011, 06:54 AM #1Member
 Join Date
 Oct 2011
 Location
 Salt Lick City, UT
 Posts
 3
 Rep Power
 0
Integer Comparison, Outputting Largest Integer Not Working
I'm writing a Java program to take three integers and output the smallest, largest, sum, and average. Everything works except for finding the highest integer. If I set number1 to 1 and number2 to 2 and number3 to 3, the program outputs 2 as largest. If I do it in this order 3, 1, 2, it puts out 3 as the largest. The bit of code to output the smallest integer works. I include it for comparison. Thanks.
Java Code:if (number1 < number2  number1 < number3) smallest = number1; else if (number2 < number1  number2 < number3) smallest = number2; else if (number3 < number1  number3 < number2) smallest = number3; if (number1 > number2  number1 > number3) largest = number1; else if (number2 > number1  number2 > number3) largest = number2; else if (number3 > number1  number3 > number2) largest = number3; System.out.printf("The smallest of the numbers is %d.\n", smallest); System.out.printf("The largest of the numbers is %d.\n", largest);
 10162011, 07:01 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,131
 Blog Entries
 7
 Rep Power
 24
Re: Integer Comparison, Outputting Largest Integer Not Working
And if you test the numbers 2, 1, 3 it outputs 2 as the smallest number; those ifstatements are completely incorrect. Have a look at the Math.min( ... ) and Math.max( ... ) methods instead.
kind regards,
Jos
ps. or if you insist of doing it all yourself, change the  (inclusive or) to && (logical and) operator.The only person who got everything done by Friday was Robinson Crusoe.
 10162011, 07:08 AM #3Member
 Join Date
 Oct 2011
 Location
 Salt Lick City, UT
 Posts
 3
 Rep Power
 0
Re: Integer Comparison, Outputting Largest Integer Not Working
Thanks, JosAH.
I didn't even know there were Math.min and Math.max methods. My book hasn't got that far into it. I'm using Deiltel's Java How to Program.
 10162011, 07:42 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,131
 Blog Entries
 7
 Rep Power
 24
Re: Integer Comparison, Outputting Largest Integer Not Working
I personally prefer the methods over a hand crafted solution; have a look, the following fragment sorts things out given the numbers a, b and c:
Java Code:int max= Math.max(Math.max(a, b), c); int min= Math.min(Math.min(a, b), c); int mid= a+b+cmaxmin;
JosThe only person who got everything done by Friday was Robinson Crusoe.
 10162011, 08:59 PM #5Member
 Join Date
 Oct 2011
 Location
 Salt Lick City, UT
 Posts
 3
 Rep Power
 0
Similar Threads

Working with Integer parseInt(String)
By Rocketz in forum New To JavaReplies: 3Last Post: 02272011, 08:31 AM 
Help with Binary recursive method to find the largest integer
By flyingcurry in forum New To JavaReplies: 12Last Post: 10312010, 07:14 PM 
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05172010, 12:50 AM 
[SOLVED] Struts 2 Integer Validation not working
By piyu.sha in forum Advanced JavaReplies: 2Last Post: 10152008, 01:54 AM 
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01122008, 11:30 PM
Bookmarks