Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Input three numbers and then sort non descending order

Sun Oct 31, 2010 8:14 pm

I have been trying to figure out this assignment for like two days but i seem to not be able to figure out the right code for it. I'm really having trouble with the if and else statement. So if you can please help me, it will be a life saver.

The purpose of this assignment is have the user input 3 numbers for a,b,c. then the output should be in non-descending order.

Code:

import java
.util.Scanner;

 public class NondescendingOrder {
     public static void main (String[] args) {

         // declares variables
         int a,b,c;
         Scanner input= new Scanner (System.in);

         // ask user for input
         System.out.print("Please enter three numbers separated by space: ");

         // read and store the input
         a= input.nextInt();
         b= input.nextInt();
         c= input.nextInt();

         //output of input
         System.out.println ("Input 1= " +a);
         System.out.println ("Input 2= " +b);
         System.out.println ("Input 3= " +c);

         // if and else statement

        if (a<b) {
                if (b<c)
                System.out.println("The order is: "+a+" "+b+" "+c);
                    else
                    System
.out.println("The order is: "+a+" "+c+" "+b);

        }
        else if (b<a){
                if (a<c)
                System.out.println("The order is: "+b+" "+a+" "+);
                    else
                    System
.out.println("The order is: "+b+" "+c+" "+a  );
        }

        else if (c<a){
                if (b<a)
                System.out.println("The order is: "+c+" "+a+" "+b);
                    else
                    System
.out.println("The order is: "+c+" "+b+" "+a);

        }


    }
}
 




Re: need help with this java assignment

Mon Nov 01, 2010 11:40 am

Code:


  if (a<=b) {
            if (b<c)
            System.out.println("The order is: "+a+" "+b+" "+c);
                else
               System.out.println("The order is: "+a+" "+c+" "+b);

      }
      else if (a<=c){
            System.out.println("The order is: "+b+" "+a+" "+c );
      }
      else {
            if (b<c)
            System.out.println("The order is: "+b+" "+c+" "+a);
               else
               System.out.println("The order is: "+c+" "+b+" "+a);

      }



Post a reply
  Related Posts  to : Input three numbers and then sort non descending order
 SQL ORDER BY     -  
 Difference in order between HashSet and ArrayList     -  
 rank order filter effects on image     -  
 How to display WooCommerce customer order details     -  
 How to change the default order on forums from ascending to     -  
 Return an array with elements in reverse order     -  
 SELECT-Group by and Order by,Having Clause,count(),Joins     -  
 getting a full line input     -  
 Only numeric input in JTextField     -  
 input suggestion using Javascript     -