java WordGame program

柔情痞子 提交于 2019-12-13 05:08:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!