33.8k views
2 votes
Mad Libs are activities that have a person provide various words, which are then used to complete a short story in unexpected (and hopefully funny) ways.Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is quit 0.Ex: If the input is:apples 5shoes 2quit 0the output is:Eating 5 apples a day keeps the doctor away.Eating 2 shoes a day keeps the doctor away.Note: This is a lab from a previous chapter that now requires the use of a loop.import java.util.Scanner;public class LabProgram {public static void main(String[] args) {/* Type your code here. */}}

User Dodgio
by
4.2k points

1 Answer

1 vote

Answer:

I am writing a C++ and JAVA program.

import java.util.Scanner; //for using input output functions

public class LabProgram { // class name

public static void main(String[] args) {//start of main function body

Scanner input = new Scanner(System.in);

// creates input instance of Scanner type

String str; // declares String type variable str for a string value

int num; // declares integer type variable num to hold an integer value

str = input.next(); //scans and reads input string from user

num = input.nextInt(); //scans and reads input integer from user

//while loop continues to execute until user enters quit 0

while(str!="quit" && num!=0) {

/* prints the following message, for example if value of num = 2 and value of str = apples then the following print statement prints Eating 2 apples a day keeps the doctor away. */

System.out.println("\\Eating " + num +" " + str + " a day keeps the doctor away.");

//takes string and integer as input again and keep taking input until user //enters quit 0

str = input.next();

num = input.nextInt(); } } }

Step-by-step explanation:

The program is well explained in the comments mentioned with each statement of the program. The program simply prompts user to enter a string and an integer. The while loop keeps executing until user enters quit 0. The program keeps taking input string and integer from user and prints the message System.out.println("\\Eating " + num +" " + str + " a day keeps the doctor away."); The loop breaks when the use enters quit 0. The screenshot of the program along with its output is attached.

Mad Libs are activities that have a person provide various words, which are then used-example-1
User Dsuess
by
4.6k points