Spaces:
Running
Running
File size: 1,335 Bytes
777b321 e11bd8c 777b321 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
Write the method
public static void printSeveral(String str, int amount)
which takes a string and a number as parameters.
The method prints the given string to a single stack a given number of times.
Example on calling the method:
public static void main(String[] args) {
printSeveral("xy", 4);
printSeveral("bye",3);
}
Program outputs:
xyxyxyxy
byebyebye
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
Object[][] p = {{"abc", 3}, {"hello ",4}, {"*xyz",5}, {"-",15}};
for (Object[] param : p) {
System.out.println("Testing with parameters \"" + param[0] + "\", " + param[1]);
printSeveral((String) param[0], (Integer) param[1]);
System.out.println("");
}
}
//CONTINUE HERE
public static void printSeveral(String str, int amount){
for (int i=1; i<=amount; i++) {
System.out.print(str);
}
System.out.println(); // Add newline after printing the repeated string
}
}
Testing with parameters "abc", 3
abcabcabc
Testing with parameters "hello ", 4
hello hello hello hello
Testing with parameters "*xyz", 5
*xyz*xyz*xyz*xyz*xyz
Testing with parameters "-", 15
---------------
|