Difference between revisions of "Print Numbers"
From CompSciWiki
(All your doing is printing n1....) |
|||
Line 34: | Line 34: | ||
else if( n2 <= n3 ) | else if( n2 <= n3 ) | ||
{ | { | ||
− | System.out.print( | + | System.out.print( n2 ); |
} | } | ||
else | else | ||
{ | { | ||
− | System.out.print( | + | System.out.print( n3 ); |
} | } | ||
</pre> | </pre> | ||
Line 48: | Line 48: | ||
public static void printNumbers( int n1, int n2, int n3 ) | public static void printNumbers( int n1, int n2, int n3 ) | ||
{ | { | ||
− | printSmallest(); | + | printSmallest( n1, n2, n3 ); |
System.out.print( "," ); | System.out.print( "," ); | ||
− | printMidNum(); | + | printMidNum( n1, n2, n3 ); |
System.out.print( "," ); | System.out.print( "," ); | ||
− | printLargest(); | + | printLargest( n1, n2, n3 ); |
} | } | ||
Line 73: | Line 73: | ||
public static void printMidNum( int n1, int n2, int n3 ) | public static void printMidNum( int n1, int n2, int n3 ) | ||
{ | { | ||
− | if( n1 >= n2 && n1 <= n3 ) | + | if( n1 >= n2 && n1 <= n3 || ( n1 >= n3 && n1 <= n2 ) ) |
{ | { | ||
System.out.print( n1 ); | System.out.print( n1 ); |
Revision as of 16:33, 8 April 2010
Back to the Program-A-Day homepage
ProblemWrite a method which will take 3 ints as parameters.
The method should print 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 |