java WordGame程序

时间:2014-04-05 18:43:48

标签: java swing joptionpane

我完全是初学者。这是一个学校项目。我只需要知道,有谁能告诉我为什么这不会运行?

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);
   }
}

2 个答案:

答案 0 :(得分:0)

考虑到你的系统上安装了JRE / JDK

创建一个名为" WordGame.java"的文件把它放在一个目录中说D:/Test

现在转到命令提示符并运行javac WordGame.java这将编译代码并生成类文件

现在运行SET CLASS_PATH=.临时设置当前目录为classpath

然后运行java WordGame然后你去......你会得到你想要的东西

答案 1 :(得分:0)

我在我的电脑上运行你的程序,运行得很好。您的代码唯一的问题是:

 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!";

您应该输出用户输入的城市名称,而不是说CITY。否则,您的代码正常工作! :)