Ask Question
3 October, 09:27

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.

Ex: If the input is:

apples 5

shoes 2

quit o

the output is:

Eating 5 apples a day keeps the doctor away.

Eating 2 shoes a day keeps the doctor away.

Note: This requires the use of a loop.

+5
Answers (1)
  1. 3 October, 09:30
    0
    The program to this question can be defined as follows:

    Program:

    import java. util.*; / /import package

    public class Main / /defining class

    {

    public static void main (String[] as) / /defining main method

    {

    Scanner ax = new Scanner (System. in); //create Scanner class object for user-input

    String val; / /defining String variable

    int x; //defining an integer variable

    System. out. print ("Enter string value first then numeric, and for exit 'quit & 0': / n");

    val = ax. next (); / /input value

    x = ax. nextInt (); //input value

    while (! (val. equals ("quit") && x==0)) / /Check value

    {

    System. out. println ("Eating "+x+" "+val+" a day keeps the doctor away."); / /print value

    val = ax. next (); / /input value

    x = ax. nextInt (); / /input value

    }

    }

    }

    Output:

    Enter string value first then numeric, and for exit 'quit & 0':

    apple 5

    Eating 5 apple a day keeps the doctor away.

    shoes 2

    Eating 2 shoes a day keeps the doctor away.

    quit 0

    Explanation:

    In the above program, two variable "val" and "x" is declared, in which "val" is string variable and "x" is integer variable, in the next line, a while loop is declared, which can be described as follows:

    In the loop, a condition is checked, that "val" is equal to "quit" and x is equal to "0", inside the loop, it will input value from the user-end.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Mad Libs are activities that have a person provide various words, which are then used to complete a short story in unexpected (and ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers