Question

Ask the user to enter a message Ask the user how many time they want to...

  1. Ask the user to enter a message
  2. Ask the user how many time they want to print the message
  3. Then use "while","do-while" and "for" loop to print the message. Show the number at the beginning of the lines when printing.

For example:

Enter your message: Welcome to Java

How many time you want to print this message: 3

Using "while" loop:

1=> Welcome to Java

2=> Welcome to Java

3=> Welcome to Java

Using "do-while" loop:

1=> Welcome to Java

2=> Welcome to Java

3=> Welcome to Java

Using "for" loop:

1=> Welcome to Java

2=> Welcome to Java

3=> Welcome to Java

  • Put at least two outputs (two different numbers and results)  (results after you run your code) at the end of your code as a multi-line comment.

/*

Your result

*/

  • Don't forget to put your name and a short description of your code on the top on your code.  
  • use JAVA
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Description : first we read the message and then the message that how many times do you want to print, then we will be printing using 3 loops, that means using the for loop, while loop and do-while loop


import java.util.*;

public class Main
{
   public static void main(String[] args) {
      
       Scanner s = new Scanner(System.in);
       System.out.print("Enter your message : ");
       System.out.println("");
       String str = s.nextLine(); // read the message
       System.out.print("How many time you want to print this message : ");
       System.out.println("");
       int n = s.nextInt(); // read how many times do you want to print
       System.out.println("Using \"while\" loop:");
       int j = 0;
       while(j < n) // print using while loop
       {
       System.out.println((j + 1) +"=> " + str );
       j++;
       }
       System.out.println("Using \"do-while\" loop:");
       int k = 0;
       do // print using do while loop
       {
       if(n == 0)
       {
       break;
       }
       System.out.println((k + 1) +"=> " + str );
       k++;
       }while(k < n); // print using while loop
       System.out.println("Using \"for\" loop:");
       for(int i = 0; i < n; i++)
       {
       System.out.println((i + 1) +"=> " + str );
       }
   }
}
Output :

Add a comment
Know the answer?
Add Answer to:
Ask the user to enter a message Ask the user how many time they want to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java programming!!! Write the code fragment that will do the following: • Ask the user how...

    java programming!!! Write the code fragment that will do the following: • Ask the user how many numbers they want to enter • Declare and instantiate an array of doubles with as many elements as the number entered by the user • Write one 'for' loop to fill the array with data from the user • Write a second 'for' loop to calculate the sum of all of the numbers in the array • Print the calculated sum Note: Write...

  • Display a welcome message and then ask the user to enter his/her name Display a personal...

    Display a welcome message and then ask the user to enter his/her name Display a personal welcome message by addressing the user by his/her name. Declare a constant and assign a number to it as the Conference Room capacity. Ask the user how many people are attending the meeting. Display a meaningful message for each of the three different scenarios. The message informs the user whether he/she can have the requested room and also displays the number of people that...

  • Use the pseudocode below to create your program: Ask the user to Enter their marks. In...

    Use the pseudocode below to create your program: Ask the user to Enter their marks. In the while loop condition, IF the score entered is >=60, then print “Congratulations! Passed” After printing “Congratulations! Passed””, ask the user to enter the mark again. When the above while condition is not met then print “Sorry! Fail”

  • Write Java code that does the following Ask the user to enter a file name. Ask...

    Write Java code that does the following Ask the user to enter a file name. Ask the user for two integers as the lower bound and the upper bound. Use a loop to write all the odd numbers between the lower bound and the upper bound (inclusive) to the file, and then close the file. Use either while loop, do while loop, or for loop to complete the program. PrintWriter class is used. The file is closed before the program...

  • 1. print mesage to user to prompt user to enter 10 numbers 2 have program accept...

    1. print mesage to user to prompt user to enter 10 numbers 2 have program accept 10 numbers from user and populate 10 occurrances of an array (use a loop to do this). 3. print message to user to advise user contents of array follows 4. have program display array contents (use loop to do this) end program comment in java please

  • Create a Java program which asks the user how many names they want to enter. The...

    Create a Java program which asks the user how many names they want to enter. The program should then instantiate an array that will hold this many names. The main method should call a method, getNames() to allow the user to enter the names. A for loop should be used in this method. Once the names are entered, the main method should call another method displayNames() which will use a while loop to display the names entered.

  • This question deals with extending already existing Java code via a while loop. Ask the user...

    This question deals with extending already existing Java code via a while loop. Ask the user how many year inputs he wants to check - an integer. Assume that the user always gives an input which is between 1 and 5 (inclusive). Next, ask the user for K number of year inputs where K = the number user has given as input before (inside a while loop). Check for each year input whether that year is a leap year or...

  • Write a program that allows the user to enter a series of exam scores. The number...

    Write a program that allows the user to enter a series of exam scores. The number of scores the user can enter is not fixed; they can enter any number of scores they want. The exam scores can be either integers or floats. Then, once the user has entered all the scores they want, your program will calculate and print the average of those scores. After printing the average, the program should terminate. You need to use a while loop...

  • First, ask the user for a positive integer called “length”. If the user does not provide...

    First, ask the user for a positive integer called “length”. If the user does not provide an integer, or if the integer is not positive, then ask again, by asking inside a while loop. The acceptable integer is intended to indicate the user’s desired twist length. A twist is always three lines long, and made of slash characters and X characters. For example, if length is 25, then your code should print out this: Simple output: IXI XI XI XI...

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT