You can create a method. For instance it can be implemented like that.
public void printWords(String[] wordsArray, int repeatPhraseNumber) {
for(int i=0;i<wordsArray.length;i++) {
for(int repeatCount=0;repeatCount<repeatPhraseNumber;repeatCount++) {
System.out.println(wordsArray[i]);
}
}
}
And you can call this like that, wordConsolePrinter
is an instance of a class that has the printWords
method.
You can call it as follows.
String[] words = {"Hello","World"};
wordConsolePrinter.printWords(words,5);
And this will produce the following output.
Hello
Hello
Hello
Hello
Hello
World
World
World
World
World
If you need to print all words as a single sentence/phrase, the method should be modified as follows.
public void printWords(String[] wordsArray, int repeatPhraseNumber) {
for(int repeatCount=0;repeatCount<repeatPhraseNumber;repeatCount++) {
for(int i=0;i<wordsArray.length;i++) {
System.out.print(wordsArray[i]);
System.out.print(" "); // Space between words
}
System.out.println(""); // New line
}
}
And this will output
Hello World
Hello World
Hello World
Hello World
Hello World
Of course this could be implemented without using methods.
This is a really easy task, please try to read more about loops and arrays.