Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How Compare Integers using the if statement and && operator? Can it be done?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    My Mood
    Inspired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post 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...

    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
     	}
     }


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default 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.

    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!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. The Following User Says Thank You to snowguy13 For This Useful Post:

    sethxhero7 (January 28th, 2012)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How Compare Integers using the if statement and && operator? Can it be done?

    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.

    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.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    sethxhero7 (January 28th, 2012)

  6. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: How Compare Integers using the if statement and && operator? Can it be done?

    If you want something more elegant, try an array.

  7. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

Similar Threads

  1. [SOLVED] (Simple) How to use an if statement to respond to random integers
    By DusteroftheCentury in forum Loops & Control Statements
    Replies: 9
    Last Post: January 27th, 2012, 09:15 PM
  2. Compare 2 XML iles
    By deep in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 9th, 2011, 10:17 AM
  3. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  4. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  5. Trying to somehow Compare Generics
    By Omega_ryan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:58 PM