Hi, I have been trying to solve this problem for a while now. I wrote a program that analyzes 3 numbers entered by a user. All calculations are working fine. Now I am attempting to use 'if' statements to display the largest and smallest number entered.

I am adjusting the code to determine the smallest number entered so I will just post the code to determine the largest. I know I must not be covering all the possiblities to determine the largest and smallest numbers entered. Can someone give me some guidance?


// determines the smallest entered number

result = "";

if ( number1 > number2 && number3 > number2)
result = result + number2;

if ( number1 < number2 && number3 > number2)
result = result + number1;

if ( number1 < number2 && number3 < number1)
result = result + number3;

// determines the largest entered number

result2 = "";

if ( number1 > number2 && number3 > number1)
result2 = result2 + number3;

if ( number1 < number2 && number3 < number2)
result2 = result2 + number2;

if ( number1 > number2 && number2 > number3)
result2 = result2 + number1;

far simpler:
set largest and smallest to the first number.
if largest<second number set largest to second number
else if smallest>second number set smallest to second number
if largest<third number set largest to third number
else if smallest>third number set smallest to third number


It seems you're trying to create a String representing the number to be output?
Try using Integer.toString(int) instead. It's the standard way.

far simpler:
set largest and smallest to the first number.
if largest<second number set largest to second number
else if smallest>second number set smallest to second number
if largest<third number set largest to third number
else if smallest>third number set smallest to third number


It seems you're trying to create a String representing the number to be output?
Try using Integer.toString(int) instead. It's the standard way.

Thanks for writing back. After posting I continued working on it I think I figured it out. Maybe not as simply as you mention, but it's working..so far.


// determines the smallest entered number

result = "";

if ( number1 < number2 && number2 < number3)
result = result + "\nThe lowest number is " + number1 + "\nThe highest number is " + number3;

if ( number1 < number2 && number2 > number3)
result = result + "\nThe lowest number is " + number3 + "\nThe highest number is " + number2;

if ( number1 > number2 && number2 < number3)
result = result + "\nThe lowest number is " + number2 + "\nThe highest number is " + number3;

if ( number1 > number2 && number2 > number3)
result = result + "\nThe lowest number is " + number3 + "\nThe highest number is " + number1;

If you don't have to use the 'if' way, you could always throw them in an array and sort them

int[] nums = {a,b,c,d,e};

Arrays.sort(nums);
int x = nums.length;
System.out.println(nums[0] + " " + nums[x]);

large = (number1 > number2 ? number1 : number2)
large = (large > number3 ? large : number3)

small = (number1 < number2 ? number1 : number2)
small = (small < number3 ? small : number3)

This article has been dead for over six months. Start a new discussion instead.