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 4 of 4

Thread: arranging high to low

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default arranging high to low

    im a complete noob. i know this is a completely inefficient code, but im curious as to why this program doesnt work. Basically suppose to organize large integers to small integers. it only works if i input 5,4,3,2,1, but stops when i input in other order. essentially only the first set of if statements works..

    can someone point out how to fix it ... in something i can easily understand.

    im learning from the book java how to program 9th edition deital..
    chapter 2, Write an application that reads five integers and determines
    and prints the largest and smallest integers in the group. Use only the programming techniques you learned in this chapter.


     
    import java.util.Scanner;
     
    public class Arrange
    {
    	public static void main(String [] args)
    	{
    		Scanner input = new Scanner(System.in);
     
    		int number1;
    		int number2;
    		int number3;
    		int number4;
    		int number5;
     
    		System.out.print("Enter your first number: ");
    		number1= input.nextInt();
     
    		System.out.print("Enter your second number: ");
    		number2= input.nextInt();
     
    		System.out.print("Enter your third number: ");
    		number3= input.nextInt();
     
    		System.out.print("Enter your forth number: ");
    		number4= input.nextInt();
     
    		System.out.print("Enter your fifth number: ");
    		number5= input.nextInt();
     
     
     
    		if (number1 > number2)	
    		if (number1 > number3)
    		if (number1 > number4)
    		if (number1 > number5)
     
    		if (number2 > number3)
    		if (number2 > number4)
    		if (number2 > number5)
     
    		if (number3 > number4)
    		if (number4 > number5)
     
    			System.out.printf("%d,%d,%d,%d,%d", number1, number2, number3, number4, number5);
     
    		else if (number2 > number3)
    		if (number2 > number4)
    		if (number2 > number5)
    		if (number2 > number1)
     
    		if (number3 > number4)
    		if (number3 > number5)
    		if (number3 > number1)
     
    		if (number4 > number5)
    		if (number4 > number1)
     
    			System.out.printf("%d,%d,%d,%d,%d", number2, number3, number4, number5, number1);
     
    		if (number3 > number4)
    		if (number3 > number5)
    		if (number3 > number1)
    		if (number3 > number2)
     
    		if (number4 > number5)
    		if (number4 > number1)
    		if (number4 > number2)
     
    		if (number5 > number1)
    		if (number5 > number2)
     
    			System.out.printf("%d,%d,%d,%d,%d", number3, number4, number5, number1, number2);
     
    		if (number4 > number5)
    		if (number4 > number1)
    		if (number4 > number2)
    		if (number4 > number3)
     
    		if (number5 > number1)
    		if (number5 > number2)
    		if (number5 > number3)
     
    		if (number1 > number2)
    		if (number1 > number3)
     
    			System.out.printf("%d,%d,%d,%d,%d", number4, number5, number1, number2, number3);
     
    		if (number5 > number1)
    		if (number5 > number2)
    		if (number5 > number3)
    		if (number5 > number4)
     
    		if (number1 > number2)
    		if (number1 > number3)
    		if (number1 > number4)
     
    		if (number2 > number3)
    		if (number2 > number4)
     
    			System.out.printf("%d,%d,%d,%d,%d", number5, number1, number2, number3, number4);
    	}
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: arranging high to low

    thanks jps,seems like a helpful website.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: arranging high to low

    I'm new to coding too but instead of using a bunch of if statements, couldn't you use a few switch statements to correctly display the string for that set of conditions. I would do it that way personally, looks cleaner. This is more of a question because I'm not positive, don't take it as harsh criticism because I'm bad at coding

Similar Threads

  1. Print Student average, high, and low grades through array?
    By MLeclerc182 in forum Object Oriented Programming
    Replies: 2
    Last Post: May 10th, 2012, 06:12 AM
  2. [Help] Arranging Objects please help
    By JoelE21101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2011, 10:37 PM
  3. Threads priority: low priority run faster than high priority
    By leonixyz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2011, 03:33 PM
  4. High / low integer computation algorithm????
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 12:40 PM
  5. JAVA HIGH LOW NUMBER GUESSING GAME
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 12th, 2011, 10:50 AM