Results 1 to 5 of 5
- 10-16-2011, 06:54 AM #1
Member
- 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);
- 10-16-2011, 07:01 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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 if-statements 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.When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-16-2011, 07:08 AM #3
Member
- 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.
- 10-16-2011, 07:42 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:int max= Math.max(Math.max(a, b), c); int min= Math.min(Math.min(a, b), c); int mid= a+b+c-max-min;
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-16-2011, 08:59 PM #5
Member
- 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: 02-27-2011, 07:31 AM -
Help with Binary recursive method to find the largest integer
By flyingcurry in forum New To JavaReplies: 12Last Post: 10-31-2010, 06:14 PM -
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
[SOLVED] Struts 2 Integer Validation not working
By piyu.sha in forum Advanced JavaReplies: 2Last Post: 10-15-2008, 01:54 AM -
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01-12-2008, 10:30 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks