问题
I am totally a beginner. This is a project for school. I just need to know, can anyone tell me why this won't run ?
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);
}
}
回答1:
considering you have JRE/JDK installed on your system
create a file named "WordGame.java
" put it in a directory say D:/Test
now go to command prompt and run javac WordGame.java
this will compile the code and generate a class file
now run SET CLASS_PATH=.
to temporary setting current directory as classpath
and then run java WordGame
and there you go.. you will get what you want
回答2:
I ran your program on my computer and it ran just fine. The only problem with your code is where it says:
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!";
Instead of saying CITY, you should output the city name entered by the user. Otherwise, your code works correctly! :)
来源:https://stackoverflow.com/questions/22885176/java-wordgame-program