To display the same statement multiple times in Java, you can use a loop, such as a for
loop or a while
loop.
Here's an example of how to use a for
loop to display the same statement multiple times:
for (int i = 0; i < 10; i++) { System.out.println("Hello, World!"); }
The above example uses a for
loop to iterate 10 times and display the "Hello, World!" statement. You can adjust the loop limit to display the statement more or fewer times.
Here's an example of how to use a while
loop to display the same statement multiple times:
int count = 0; while (count < 10) { System.out.println("Hello, World!"); count++; }
The above example uses a while
loop to iterate until the count
variable reaches 10 and display the "Hello, World!" statement. You can adjust the loop condition to display the statement more or fewer times.
Alternatively, you can use a method to encapsulate the statement and call the method multiple times.
Here's an example of how to use a method to display the same statement multiple times:
public void printHello() { System.out.println("Hello, World!"); } // ... for (int i = 0; i < 10; i++) { printHello(); }
The above example defines a printHello()
method that displays the "Hello, World!" statement, and calls the method 10 times using a for
loop. This approach can make your code more modular and easier to maintain.