Write a program that plays a word game with the user. The program should ask the
ID: 3626341 • Letter: W
Question
Write a program that plays a word game with the user. The program should ask the user to enter the following information:his or her name
his or her age
the name of a city
the name of a college
a profession
a type of animal
a pet's name
After the user has entered these items, the program should display the following story, inserting the user's input into the appropriate locations:
There once was a person named NAME who lived in CITY. At the age of AGE, NAME went to college at COLLEGE. NAME graduated and went to work as a PROFESSION. Then, NAME adopted a(n) ANIMAL named PETNAME. They both lived happily ever after!
Explanation / Answer
import javax.swing.JOptionPane; public class WordGame { public static void main(String[] args) { String name; name= JOptionPane.showInputDialog("Enter your name"); String age; age= JOptionPane.showInputDialog("Enter your age"); String city; city= JOptionPane.showInputDialog("Enter the name of a city"); String college; college= JOptionPane.showInputDialog("Enter the name of a college"); String profession; profession= JOptionPane.showInputDialog("Enter profession"); String animal; animal= JOptionPane.showInputDialog("Enter a type of animal"); String pet; pet= JOptionPane.showInputDialog("Enter a pet name"); String str="There once was a person named "+ name+ " who lived in CITY. At the age of "+age+ ", "+ name +" went to college at "+ college+" ."+ name +" graduated and went to work as a "+profession+" . Then, "+ name +" adopted a(n) "+ animal +" named "+ pet+ ". They both lived happily ever after!"; JOptionPane.showMessageDialog(null, str); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.