4.12 School's punishment
Back in the days, punishments at school used to be very different. Before, teachers used to assign extra work to students that misbehaved during class time. One of the common practices was to ask to write n number of times a specific statement that was according to the specific issue. For example, some times, the teacher asked the student to write 100 times the sentence “I don’t have to write on the class’ table”. Nowadays, students may get around that writing a program that will write those sentences for them. Consequently, write a program that will ask for an integer number and a sentence. The program then outputs n number of times the sentence that was just input.
IN JAVA.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
EDIT: updated answer as per your test program. change System.out.println(sentence); to System.out.println(sentence.trim()); if you don't want extra white space appearing before the sentence.
// Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// defining a Scanner to read user
input
Scanner scanner = new
Scanner(System.in);
// reading the number of times to
repeat
int n =
scanner.nextInt();//Integer.parseInt(scanner.nextLine());
// reading sentence
String sentence =
scanner.nextLine();
// looping for n times
for (int i = 0; i < n; i++)
{
// printing
sentence
System.out.println(sentence);
}
}
}
Get Answers For Free
Most questions answered within 1 hours.