Difference between revisions of "Print Numbers"
From CompSciWiki
(Changed to utilize the CodeBlock template) |
|||
Line 18: | Line 18: | ||
|Solution= | |Solution= | ||
You should split this up into three methods which print out the smallest, middle and largest numbers. | You should split this up into three methods which print out the smallest, middle and largest numbers. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
public static void printSmallest( int n1, int n2, int n3 ); | public static void printSmallest( int n1, int n2, int n3 ); | ||
public static void printMidNum( 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 ); | public static void printLargest( int n1, int n2, int n3 ); | ||
− | + | }} | |
You will need to use <b>if statements</b> to determine the proper | You will need to use <b>if statements</b> to determine the proper | ||
order to print out the numbers. | order to print out the numbers. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
if( n1 <= n2 && n1 <= n3 ) | if( n1 <= n2 && n1 <= n3 ) | ||
{ | { | ||
Line 39: | Line 41: | ||
System.out.print( n3 ); | System.out.print( n3 ); | ||
} | } | ||
− | + | }} | |
and similarly for the middle and largest numbers. | and similarly for the middle and largest numbers. | ||
Revision as of 16:25, 4 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, separated by a comma.
|
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 |