2.1k views
3 votes
Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call: printStrings("abc", 5); will print the following output: abcabcabcabc

User Erbi
by
3.9k points

1 Answer

6 votes

Answer:

public static void printString(String x, int y){

//Create a for loop

//How does a for loop work?

//It collects a starting point, an end point and an increment respectively....all these are passed in as arguments into the for loop

//For example, the code below starts from 0, lets say we set y=5, the loop runs from zero to 5... i.e

//Note: i++ in the loop is the increment, it means the loop increases by 1

for(int i=0; i<y; i++){

//As the loop increases, your String is printed

//So... the number of strings printed is directly proportional to the number of times your loop runs

System.out.print(x);

}

}

Step-by-step explanation:

Create a for loop

How does a for loop work?

It collects a starting point, an end point and an increment respectively....all these are passed in as arguments into the for loop

For example, the code below starts from 0, lets say we set y=5, the loop runs from zero to 5... i.e

Note: i++ in the loop is the increment, it means the loop increases by 1

As the loop increases, your String is printed

So... the number of strings printed is directly proportional to the number of times your loop runs

User Gurzo
by
3.5k points