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...
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 &&?
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 } }