Difference between revisions of "Print Array"
From CompSciWiki
(17 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | {{1010PrAD|ProblemName= | + | {{1010PrAD|ProblemName=Print Numbers |
− | |Problem= The | + | |Problem= Write a method which will take 3 ints as parameters. |
+ | The method should print the elements of the array in order, separated by a comma. | ||
+ | <BR><BR> | ||
+ | Example: | ||
+ | <BR> | ||
+ | printNumbers(5, 7, 4);<BR> | ||
+ | Output:4,5,7 | ||
+ | |||
+ | |SideSectionTitle=...by students | ||
|SideSection= | |SideSection= | ||
− | |||
− | |||
− | |||
− | |||
− | |Solution= | + | |Solution= |
+ | You should split this up into three methods which print out the smallest, middle and largest images. | ||
+ | <pre> | ||
+ | public static void printSmallest( int n1, int n2, int n3 ); | ||
+ | public static void printMiddle( int n1, int n2, int n3 ); | ||
+ | public static void printLargest( int n1, int n2, int n3 ); | ||
+ | </pre> | ||
+ | |||
+ | You will need to use <b>if statements<b> to determine the proper | ||
+ | order to print out the numbers. | ||
+ | <pre> | ||
+ | if( n1 <= n2 && n1 <= n3 ) | ||
+ | { | ||
+ | System.out.print( n1 ); | ||
+ | } | ||
+ | else if( n2 <= n3 ) | ||
+ | { | ||
+ | System.out.print( n1 ); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | System.out.print( n1 ); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | |SolutionCode= | ||
+ | <pre> | ||
+ | public static void printArray( int n1, int n2, int n3 ) | ||
+ | { | ||
+ | for( int i = 0; i < n.length; i++ ) | ||
+ | { | ||
+ | System.out.print( n[i] + "," ); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
}} | }} |
Latest revision as of 12:37, 6 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 of the array in order, separated by a comma.
|
...by students | |
---|---|---|
SolutionYou should split this up into three methods which print out the smallest, middle and largest images. public static void printSmallest( int n1, int n2, int n3 ); public static void printMiddle( int n1, int n2, int n3 ); public static void printLargest( int n1, int n2, int n3 ); You will need to use if statements<b> 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( n1 ); } else { System.out.print( n1 ); } | ||
Code |