我正在编写TBAP(短信基础冒险程序)是因为。我刚刚启动它,现在已经遇到了问题。我想做的是在输出文本中有一个介绍程序的主类。在课程结束时,系统会询问“您想在哪里继续冒险?”它有五个选项,其中三个是单独的冒险,两个是库存类。现在,我被困在第一堂冒险课上。我有一个名为path的int变量。如果path == 1,则进入幻想岛课程,继续冒险。有没有什么可以用if语句来称呼这种冒险?我用变量名和路径创建了一个构造函数,一个getter和setter方法。

Summerproject课程:

package summerproject;


import java.util.Scanner;
import static summerproject.Fanastyisland.name;
import static summerproject.Fanastyisland.path;

public class Summerproject {
private static int path;
private static String name;



public Summerproject (int path, String name)
{
    this.path = path;
    this.name = name;


}


public String getname() {
    return name;
}




public void setname(String name) {
    this.name = name;
}




public int getPath() {
    return path;
}


    public void setPath(int path) {
    this.path = path;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

System.out.println("Welcome to the adventure text program! You are the choosen one to save the universe");



    System.out.println("Press any key to continue...");
    try
    {
        System.in.read();
    }
    catch(Exception e)
    {}

    System.out.println("Welcome. You are the choose one, a legend,a becon of      hope to save the universe from the forces of evil.");
    System.out.println("Only with you skills and your great power can you destroy the evil doing world.");
    System.out.println("Please enter heros name");
    name = in.next();

    System.out.println("Okay " + name + ", lets begin our adventure!!");

    System.out.println("The world can be saved, there is hope. But in order to save the world, \n "
            + "+ you must complete 9 tasks in three diffrent places in three diffrent periods of time. The past, the present and the future.");
     System.out.println("Press any key to continue...");
    try
    {
        System.in.read();
    }
    catch(Exception e)
    {}

    System.out.println("The three places are the past in the year 1322 in Fantasy island");
    System.out.println("The present is the evil little town of Keene N.H.");
    System.out.println("And the future to the year 2567 in Space!");



    System.out.println("Where would you like to go on your adventures?");
   System.out.println(" 1). Fantasy Island");
    System.out.println(" 2). Keene");
    System.out.println(" 3). Outer space");
    System.out.println(" 4). Buy wepons or potions!");
    System.out.println(" 5). Sell wepons!");
    path = in.nextInt();

   if (path == 1)
    {

    }


}
}


这是我的幻想岛课程:

package summerproject;


import java.util.Scanner;
import static summerproject.Fanastyisland.name;
import static summerproject.Fanastyisland.path;
 public class Fanastyisland extends Summerproject {
public static String name;
    public static int path;


    public Fanastyisland (String name, int path)
    {
        super(path,name);
        name = name;
        path = path;


    }
    public String getName() {
    return name;
}




public void setName(String name) {
    this.name = name;
}




public int getPath() {
    return path;
}


    public void setPath(int Path) {
    this.path = path;
}




public static void main(String[] args)
//this is where the fantasy island        adventure begins.
{
   System.out.println("Welcome to fantasy island!!")



}
}


就像我说的,我想使用if语句调用子类,但我不知道该怎么做。如果我输入1,我想参加幻想岛课程。我还没有为冒险编程,一旦修复,我将继续,我只希望现在的输出是“ Welcome to Fantasy island!”。当我键入1时。任何帮助都将非常棒!谢谢!

最佳答案

像这样:

Summerproject adventure = null;
switch (path) {
  case 1:
    adventure = new FantasyIsland (...);
    break;
  case 2:
    adventure = new Keene (...);
    break;
  ...
  default:
    System.out.println ("Illegal choice(" & path & "): try again");
  }
  if (adventure != null) {
    adventure.play ();
    ...

10-03 00:46