How Compare Integers using the if statement and && operator? Can it be done?
Hello everyone!
I'm working some exercises using an old school book and I got stumped with one of them. With a little help, I was able to solve a previous one dealing with 3 integers...
Code Java:
import java.util.Scanner; // program uses class scanner
public class ThreeIntegerSumAvgProductSmallLarge
{
public static void main ( String args [] )
{
Scanner consoleScanner = new Scanner ( System.in );
int number1,
number2,
number3,
sum,
average,
product,
smallest = 0,
largest = 0;
System.out.print ( "Please enter 1st integer: " );
number1 = consoleScanner.nextInt ();
System.out.print ( "\nPlease enter 2nd integer: \n" );
number2 = consoleScanner.nextInt ();
System.out.print ( "Please enter 3rd integer: \n" );
number3 = consoleScanner.nextInt ();
sum = number1 + number2 + number3;
average = ( ( number1 + number2 + number3 ) / 3 );
product = number1 * number2 * number3;
if ( number1 > number2 && number2 > number3 )
largest = number1;
if ( number2 > number1 && number1 > number3 )
largest = number2;
if ( number3 > number2 && number2 > number1 )
largest = number3;
if ( number1 < number2 && number2 < number3 )
smallest = number1;
if ( number2 < number1 && number1 < number3 )
smallest = number2;
if ( number3 < number2 && number2 < number1 )
smallest = number3;
System.out.printf ( "%s%d\n", "The sum of your three numbers is: ", sum );
System.out.printf ( "%s%d\n", "The product of your three numbers is: ", product );
System.out.printf ( "%s%d\n", "Your three numbers produced an average of: ", average );
System.out.printf ( "\n%s%d\n", "The largest number out of the three is: ", largest );
System.out.printf ( "%s%d\n", "The smallest number out of the three is: ", smallest );
} // end method main
} // end class ThreeIntegerSumAvgProductSmallLarge
...but then, I came across an example that involved 5 integers... and using the same technique in the code above, I was not able to find a solution using the if statements and the && operators. Fortunately, I was able to find the answer, but using a different method (below)...does anyone know how to achieve the same thing just using the 'if', '>', '>' and &&?
Code Java:
import java.util.Scanner; // Program uses the Scanner class
public class Exercise2_24 // A workbook exercise
{
public static void main ( String args [] ) // main method that begins execution of Java program
{
//New scanner called 'numberScanner' is created from Scanner class, and takes input from System
Scanner numberScanner = new Scanner ( System.in );
// the five variables need to store inputs from user.
int int1,
int2,
int3,
int4,
int5,
// the variables to store the largest and smallest integers
big,
small;
System.out.print ( "Please enter the first integer: " ); // integer prompt
int1 = numberScanner.nextInt (); // scanner reads input from user and assigns it to int1
//assigning int1 to the smallest and largest variables...
big = int1;
small = int1;
// ...technicallly at this point, int1 is the smallest and biggest number!
System.out.print ( "\nPlease enter the second integer: " );
int2 = numberScanner.nextInt ();
// if int2 is smaller than the current biggest number then...
if ( int2 > big )
big = int2;
//...then int2 will be the largest # of them all!
// however...
// if the int2 is smaller than the current smallest number then...
if ( int2 < small )
small = int1;
//...then int2 will be the littlest # of them all! (and the cycle repeats...)
System.out.print ( "\nPlease enter the third integer: " );
int3 = numberScanner.nextInt ();
if ( int3 > big ) big = int3;
if ( int3 < small ) small = int3;
System.out.print ( "\nPlease enter the fourth integer: " );
int4 = numberScanner.nextInt ();
if ( int4 > big ) big = int4;
if ( int4 < small ) small = int4;
System.out.print ( "\nPlease enter the fifth integer: " );
int5 = numberScanner.nextInt ();
if ( int5 > big ) big = int5;
if ( int5 < small ) small = int5;
// Prints out numbers entered by user
System.out.printf ( "\nThe integers inputted by the user are: %d%s%d%s%d%s%d%s%d",
int1, ", ", int2, ", ", int3, ", ", int4, ", & ", int5 );
System.out.printf ( "\nThe largest number entered is: %d", big ); // prints the biggest
System.out.printf ( "\nThe smallest number entered is: %d", small ); // prints the smallest
}
}
Re: How Compare Integers using the if statement and && operator? Can it be done?
I think the way you're doing this right now works very well, and the use of a complex if statement comparing all five numbers at once would just be messy and hard to understand. Try to imagine a way that you'd compare all five at once: it'd just be a mess! If number 1 is greater than numbers 2, 3, 4, and 5, then do this, otherwise if number 2 is greater than numbers 1, 3, 4, and 5, do this, but otherwise, etc... You get the idea. :P
So, I don't think you need to make any changes to how you're determining the max and min now. Just a note: you could alternatively use the Math.max() and Math.min() methods.
Happy programming! :D
Re: How Compare Integers using the if statement and && operator? Can it be done?
Quote:
does anyone know how to achieve the same thing just using the 'if', '>', '>' and &&?
What's wrong with what you came up with in the other thread? It looks OK to me.
Code :
if ( int1 > int2 && int1 > int3 && int1 > int4 && int1 > int5 ) big = int1;
-----
I agree with snowguy13, though, what you have now is better: clearer and more suitable for extension to any number of inputs.
Re: How Compare Integers using the if statement and && operator? Can it be done?
If you want something more elegant, try an array.
Re: How Compare Integers using the if statement and && operator? Can it be done?
I agree with marylandfour. If you put all the numbers in to an array, you could avoid conditionals all together and just use Arrays.sort() method.
Re: How Compare Integers using the if statement and && operator? Can it be done?
I think (or at any rate, am assuming) that the idea behind the code was to get used to conditionals and flow of control constructs. Rather than avoiding them with a library method.
But for elegance, yes, Arrays.sort(). Or, if you favour a less retro look, try the collections framework and a List of some sort.