This program asks a user how many times to print a line of text and then executes the input.
My Code
import java.util.Scanner;
public class ManyPrints {
// NOTE: do not change the method definition, e.g. add parameters to method
public static void printText() {
// Write your code here
System.out.println("In the beginning there were the swamp, the hoe and Java.");
}
public static void main(String[] args) {
// ask the user how many times the text should be printed
// use the while structure to call the printText method several times
Scanner reader = new Scanner(System.in);
System.out.println("How many?");
int input = Integer.parseInt(reader.nextLine());
int tic = 0;
while (tic < input) {
printText();
tic++;
}
}
}
Model Code
import java.util.Scanner;
public class ManyPrints {
// NOTE: do not change the method definition, e.g. add parameters to method
public static void printText() {
// Write your code here
System.out.println("In the beginning there were the swamp, the hoe and Java.");
}
public static void main(String[] args) {
// ask the user how many times the text should be printed
// use the while structure to call the printText method several times
Scanner reader = new Scanner(System.in);
System.out.println("How many?");
int count = Integer.parseInt(reader.nextLine());
while (count > 0) {
count--;
printText();
}
}
}
Comments
Using — to subtract down to zero in the loop is a nice way of not having to include another variable.