Difference between revisions of "Print Numbers"
From CompSciWiki
(Changed the introduction to be a little more specific and added an OutputBlock) |
(Completely changed the Solution Code. Old version was broken and incomplete. New version is hot, hot, hot) |
||
Line 58: | Line 58: | ||
|SolutionCode= | |SolutionCode= | ||
− | + | import java.util.Scanner; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | /************************************************ | |
− | + | PrintNumbers | |
− | + | ||
− | + | Purpose: This program will take 3 integers as input from the user, and then will | |
− | + | re-order them and print the result | |
− | + | ||
− | + | ************************************************/ | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | public static void | + | public class PrintNumbers{ |
− | { | + | public static void main(String []args){ |
− | + | ||
− | + | Scanner keyboard = new Scanner(System.in); //create Scanner object | |
− | System.out. | + | |
− | + | processNums(keyboard) ; | |
− | + | ||
− | + | endProgram() ; | |
− | System.out. | + | |
− | + | } | |
− | + | ||
− | + | //Method: processNums | |
− | System.out. | + | //Purpose: Accepts three integers as input from the user, and passes |
− | + | // these numbers to reorderAndPrint() | |
− | + | ||
− | + | public static void processNums(Scanner keyboard) { | |
− | + | ||
− | + | int numberOne ; | |
− | + | int numberTwo ; | |
− | + | int numberThree ; | |
− | System.out. | + | |
− | + | System.out.println("Enter the first number: ") ; | |
− | + | numberOne = keyboard.nextInt() ; | |
− | + | System.out.println("Enter the second number: ") ; | |
− | System.out. | + | numberTwo = keyboard.nextInt() ; |
− | + | System.out.println("Enter the third number: ") ; | |
− | + | numberThree = keyboard.nextInt() ; | |
− | + | ||
− | + | reorderAndPrint(numberOne, numberTwo, numberThree) ; //passes the three numbers to reorderAndPrint() | |
− | + | ||
+ | } | ||
+ | |||
+ | //Method: reorderAndPrint | ||
+ | //Purpose: Accepts three numbers, reorders them, and prints the result | ||
+ | |||
+ | public static void reorderAndPrint(int one, int two, int three) { | ||
+ | |||
+ | if(one <= two && two <= three) | ||
+ | System.out.println("Numbers: " + one + ", " + two + ", " + three) ; | ||
+ | |||
+ | else if(two <= one && one <= three) | ||
+ | System.out.println("Numbers: " + two + ", " + one + ", " + three) ; | ||
+ | |||
+ | else if(three <= two && two <= one) | ||
+ | System.out.println("Numbers: " + three + ", " + two + ", " + one) ; | ||
+ | |||
+ | else if(one <= three && three <= two) | ||
+ | System.out.println("Numbers: " + one + ", " + three + ", " + two) ; | ||
+ | |||
+ | else if(two <= three && three <= one) | ||
+ | System.out.println("Numbers: " + two + ", " + three + ", " + one) ; | ||
+ | |||
+ | else | ||
+ | System.out.println("Numbers: " + three + ", " + one + ", " + two) ; | ||
+ | |||
+ | } | ||
+ | |||
+ | public static void endProgram() { | ||
+ | |||
+ | System.out.println() ; | ||
+ | System.out.println("Programmed by A. Student") ; | ||
+ | System.out.println("**End of Program**") ; | ||
+ | |||
+ | } | ||
} | } | ||
}} | }} |
Revision as of 14:26, 7 December 2011
Back to the Program-A-Day homepage
ProblemWrite a method which will take 3 integers as parameters.
The method should print out the elements in order from smallest to largest, separated by a comma.
Enter the first number: 40 Enter the second number: 32 Enter the third number: 11 Numbers: 11, 32, 40 Programmed by A. Student **End of Program** |
Static Methods | |
---|---|---|
SolutionYou should split this up into three methods which print out the smallest, middle and largest numbers. public static void printSmallest( int n1, int n2, int n3 ); public static void printMidNum( int n1, int n2, int n3 ); public static void printLargest( int n1, int n2, int n3 ); You will need to use if statements to determine the proper order to print out the numbers. if( n1 <= n2 && n1 <= n3 ) { System.out.print( n1 ); } else if( n2 <= n3 ) { System.out.print( n2 ); } else { System.out.print( n3 ); } and similarly for the middle and largest numbers. | ||
Code |