Question: write a main program that will prompt the user for a message to be printed to the terminal and how many times (numRows) the message
write a main program that will prompt the user for a message to be printed to the terminal and how many times (numRows) the message will be printed. The number of times will be validated and the prompt will be displayed to user until the user enters a valid input. The message will then be printed as many times as user desired using 3 different techniques: using a while loop, do-while loop, and a for-loop. Prompting user can be done using Scanner or JOptionPane, your choice.
Program Flow:
Write a main program within a new Class called CountingLoops that will:
Prompt User of Input:
Step 1: Prompt the user to enter the message to be printed multiple times.
Print Enter the message you want to print? .
Step 2: Read the input message from the keyboard. Store the input into a variable called message. What data type do you think it should be?
Step 3: Prompt the user to enter the number of times the message should be printed.
Print Enter how many times you want your message to be printed. Enter value from 1-20: .
Step 4: Store the input into variable called numRows. What data type do you think it should be?
Step 5: Validate the number entered, numRows, to make sure it is 1-20.
Step 6: While the number is not Valid (number from 1-20) then print an error message to the console that the number was not between 1-20.
Print: Number was not between 1-20. Try Again.
Then prompt the user to enter the number of times the message should be printed.
Print Enter how many times you want your message to be printed. Enter value from 1-20: .
Step 7: using a while loop or do-while loop, repeat the validation loop until the user enters a number from 1-20.
Once the user enters a Valid number from 1-20 Then Display Output
Display Output:
Print message multiple times using while loop
Step 8: Print message stating Output using while loop
Step 9: using a while loop, print the message as many times as requested by the user, numRows times. Within the loop, print message row as:
Row XX -
XX is the row number. Use a variable as an iterator to keep track of the row number. Increment row number each time the row is printed until the max number of rows is printed.
Step 10: After loop has terminated, Print message stating how many times in loop:
Times in while loop: XX
Print message multiple times using do-while loop
Step 11: Print message stating Output using do-while loop
Step 12: using a do-while loop, print the message as many times as requested by the user, numRows times. Within the loop, print message row as:
Row XX -
XX is the row number. Use a variable as an iterator to keep track of the row number. Increment row number each time the row is printed until the max number of rows is printed.
Step 13: After loop has terminated, Print message stating how many times in loop:
Times in do-while loop: XX
Print message multiple times using for loop
Step 14: Print message stating Output using for loop
Step 15: using a for loop, print the message as many times as requested by the user, numRows times. Within the loop, print message row as:
Row XX -
XX is the row number. Use a variable as an iterator to keep track of the row number. Increment row number each time the row is printed until the max number of rows is printed.
Step 16: After loop has terminated, Print message stating how many times in loop:
Times in for loop: XX
Sample Output:
Make sure to print the ROW# starting with row 1. The bold text is what is entered on keyboard:
Enter the message you want to print?: Good Morning Rowan U[ENTER]
Enter how many times you want your message printed. Enter number from 1-20: 0[ENTER]
Number was not between 1-20. Try Again.
Enter how many rows you want to print. Enter number from 1-20: 100[ENTER]
Number was not between 1-20. Try Again.
Enter how many times you want your message printed. Enter number from 1-20: 3[ENTER]
Output using while loop . . .
Row 1 - Good Morning Rowan U
Row 2 - Good Morning Rowan U
Row 3 - Good Morning Rowan U
Times in while loop: 3
Output using do-while loop . . .
Row 1 - Good Morning Rowan U
Row 2 - Good Morning Rowan U
Row 3 - Good Morning Rowan U
Times in do-while loop: 3
Output using for loop . . .
Row 1 - Good Morning Rowan U
Row 2 - Good Morning Rowan U
Row 3 - Good Morning Rowan U
Times in for loop: 3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
