139k views
1 vote
Create a new method called printHellos(), that displays a series of "Hello World #" messages to the console.

It has a single int parameter (n) for the number of hellos, and returns nothing (void), but displays output to console
TEST: Test your method with simple calls in the main(), to ensure your implemented is correct. Try it with small number of messages first, but then also with a big number (1000) -- have fun!
Example calls in main():
int count = 4;
printHellos (count); // display 4 "Hello World #" messages
printHellos (2); // display 2 "Hello World #" messages
printHellos (0); // display 0 "Hello World #" messages, so display nothing

User Nburk
by
7.9k points

1 Answer

4 votes

Final answer:

The question is about creating a Java method named printHellos() to print 'Hello World #' messages a given number of times. The method uses a for loop and System.out.println() to achieve this. It is tested in the main() method with different numbers of messages.

Step-by-step explanation:

The student is asking for help with creating a method in a programming language, likely Java, which is used to print out a number of Hello World messages to the console. The method should be named printHellos() and take an int parameter that determines how many times the message is printed. Here's an example of how this method could be implemented in Java:

public void printHellos(int n) {
for (int i = 1; i <= n; i++) {
System.out.println("Hello World #" + i);
}
}

To test the method, you can call it in the main() method like this:

public static void main(String[] args) {
printHellos(4); // Prints 4 messages
printHellos(2); // Prints 2 messages
printHellos(0); // Prints nothing
// For a large number:
printHellos(1000); // Prints 1000 messages
}

User BadHorsie
by
8.8k points