KaiquanMah's picture
Update Week 2: Methods, strings and lists/3B. Print several
e11bd8c verified
raw
history blame
1.34 kB
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
---------------