Java Programming HELP!!?
September 21st, 2009 | by admin |Write a program that plays a word game with the user. The program should ask the user to enter the following:
•His or her name
•His or her age
•The name of the city
•The name of the 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!
so far I have written this but I am stocked here:
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");
Please help me finished this program and check what I have done, maybe I have a few errors.
Thanks in advance!!!!
Nothing was wrong buddy!
code was just right ,you needed to add sysout thats it..
Here’s the code
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+
" \n who lived in CITY. At the age of "+age+
", "+ name +" went to college at \n"+ college+" ."+ name
+" graduated and went to work as a \n"+profession+" . Then, "+ name
+" adopted a(n) "+ animal +" \n named "+ pet+
". They both lived happily ever after!";
JOptionPane.showMessageDialog(null, str);
}
}
Cheers:)
2 Responses to “Java Programming HELP!!?”
By ♥Neeraj Yadav♥ on Sep 21, 2009 | Reply
Nothing was wrong buddy!
code was just right ,you needed to add sysout thats it..
Here’s the code
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+
" \n who lived in CITY. At the age of "+age+
", "+ name +" went to college at \n"+ college+" ."+ name
+" graduated and went to work as a \n"+profession+" . Then, "+ name
+" adopted a(n) "+ animal +" \n named "+ pet+
". They both lived happily ever after!";
JOptionPane.showMessageDialog(null, str);
}
}
Cheers:)
References :
By deonejuan on Sep 21, 2009 | Reply
Yeah, I was going to say the same thing. You can do one thing.
String pet = JOptionPane.showInputDialog("Enter …);
instead of two lines like you’ve got it.
Do that for every dialog.
.showInputDialog( 1 arg) or optionally some more args
.showMessageDialog( null, 1 arg ) or optionally some more
the null is does Dialog belong to a JFrame or the Screen.
Normally, we check to see if the input was blank, but it is not required.
but a fun question. (Give points to above dude if you award points)
References :