java WordGame程序

问题描述 投票:0回答:3

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

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);
   }
}
java swing joptionpane
3个回答
0
投票

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

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

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

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

然后运行java WordGame,然后就可以了。您将得到想要的东西


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,您应该输出用户输入的城市名称。否则,您的代码将正常工作! :)


0
投票

许多网站没有显示此错误。

使用此网站进行编程:

http://www.repl.it/languages/java10

© www.soinside.com 2019 - 2024. All rights reserved.