Difference between revisions of "Print Numbers"
From CompSciWiki
(Changed to utilize the CodeBlock template) |
(Changed the introduction to be a little more specific and added an OutputBlock) |
||
Line 2: | Line 2: | ||
|Problem= Write a method which will take 3 integers as parameters. | |Problem= Write a method which will take 3 integers as parameters. | ||
− | The method should print out the elements in order, separated by a comma. | + | The method should print out the elements in order from smallest to largest, separated by a comma. |
<BR><BR> | <BR><BR> | ||
− | + | For example, if the numbers input by the user are 40, 32, and 11, the program would have this output: | |
<BR> | <BR> | ||
− | + | {{OutputBlock | |
− | + | |Code= | |
+ | 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** | ||
+ | }} | ||
+ | |||
Revision as of 14:23, 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 |