【问题标题】:java WordGame programjava文字游戏程序
【发布时间】:2014-04-05 18:43:47
【问题描述】:

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

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

【问题讨论】:

  • 请描述问题。什么不起作用?为什么?发生了什么?你期望会发生什么?
  • 您看到了什么错误或奇怪的行为?
  • 这是我得到的错误。编译错误时间:0 内存:0 信号:0 Main.java:2:错误:WordGame 类是公共的,应该在一个名为 WordGame.java 的文件中声明 public class WordGame
  • 我正在使用 ideone.com 来测试它。我不知道这是否是问题的一部分。
  • @user2302865 我会说是的,这就是问题所在。它似乎无法分配内存。此外,它无法保存文件。

标签: java swing joptionpane


【解决方案1】:

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

创建一个名为“WordGame.java”的文件,将它放在一个目录中,比如D:/Test

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

现在运行SET CLASS_PATH=. 将当前目录临时设置为类路径

然后运行java WordGame 就可以了......你会得到你想要的

【讨论】:

    【解决方案2】:

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

     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。否则,您的代码将正常工作! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-12
      • 2015-03-07
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      相关资源
      最近更新 更多