制作一个简单的基于文本的RPG比预期更难

时间:2016-04-14 04:49:05

标签: java if-statement notepad++ javac

Sooooo ...我正在使用JAVA构建基于文本的RPG,但有些事情是错误的.... 它会编译,它会运行,但是......好吧,很难解释。我会把代码放在第一位。

    import java.util.Scanner;

public class XOBYT
{

    public static void main(String[] args)
    {
        String choice;
        Scanner scan = new Scanner(System.in);
        System.out.println("COPYRIGHT 2016 HARBNGER STUDIOS\nYou wake up in a broken-down alabaster\nhouse with no knowledge of who\nyou are or where you came from.\nWhich direction do you want to head? (options: NORTH, SOUTH, EAST, WEST)");
        choice = scan.nextLine();{
            if(choice.equals("NORTH")){System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED NORTH TO A RUN-DOWN VILLAGE. CARCASSES EVERYWHERE.\nNO SURVIVORS. I WONDER WHAT HAPPENED HERE.\nI LOOTED THE BODIES, TALOS BLESS MY SOUL. I FOUND A PISTOL AND SOME GAUZE. I CAN VIEW MY INVENTORY, HIT THE ROAD, OR LINGER AROUND.\n\n(options: INVENTORY,ROAD,LINGER)");}
            choice = scan.nextLine();{
                if(choice.equals("INVENTORY")){
                System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644\n\nI TRAVELED SOUTH AND STOPPED AT AN ABANDONED\nOUTPOST. IT HAS SOME SUPPLIES. I PICKED\nUP A PISTOL AND SOME NANITE SYRINGES. I CAN CHECK\nMY INVENTORY, MOVE FARTHER SOUTH, OR EXPLORE.\n\n(options: INVENTORY,FARTHER,EXPLORE)");}
                choice = scan.nextLine();{
                }
                if(choice.equals("FARTHER")){
                System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED EAST AND STOPPED BY A DIRTY STREAM.\nIT'S FILLED WITH GARBAGE, BUT THERE ARE ALSO PLASTIC BOTTLES.\nSHOULD I FILL THEM WITH WATER OR MOVE ON?\n\n(options: FILL,MOVE)");}
                choice = scan.nextLine();{
                }
                if(choice.equals("EXPLORE")){
                System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED WEST AND FOUND AN ABANDONED SHACK.\nSHOULD I SEARCH IT FOR LOOT OR MOVE ON?\n\n(options: SEARCH,MOVE)");}}
                choice = scan.nextLine();{}

            if(choice.equals("SOUTH")){
                System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644\n\nI TRAVELED SOUTH AND STOPPED AT AN ABANDONED\nOUTPOST. IT HAS SOME SUPPLIES. I PICKED\nUP A PISTOL AND SOME NANITE SYRINGES. I CAN CHECK\nMY INVENTORY, MOVE FARTHER SOUTH, OR EXPLORE.\n\n(options: INVENTORY,FARTHER,EXPLORE)");}
            choice = scan.nextLine();{
            }
            if(choice.equals("EAST")){
                System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED EAST AND STOPPED BY A DIRTY STREAM.\nIT'S FILLED WITH GARBAGE, BUT THERE ARE ALSO PLASTIC BOTTLES.\nSHOULD I FILL THEM WITH WATER OR MOVE ON?\n\n(options: FILL,MOVE)");}
            choice = scan.nextLine();{
            }
            if(choice.equals("WEST")){
                System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED WEST AND FOUND AN ABANDONED SHACK.\nSHOULD I SEARCH IT FOR LOOT OR MOVE ON?\n\n(options: SEARCH,MOVE)");}}
            choice = scan.nextLine();{
            }
    }
}

我似乎无法弄明白,但我认为“if”语句相互冲突。当我运行它时,键入“NORTH”,然后键入“FARTHER”或“EXPLORE”,命令提示符变为空白。它不会做它应该做的......除非你输入“INVENTORY”。然后它完美地运作。如果我将“choice equals INVENTORY”的位置与任何其他“Choice equals”切换,它会再次发生,除非它再次成为顶部的“if”语句。我在Notepad ++上键入它并通过命令提示符进行编译。提前谢谢。

编辑:根据一些评论,我忘记了,我忽略了添加我对java的生锈,我需要具体细节。如果可以的话,像婴儿一样带我走过去。如果麻烦太大,请跳过我的问题。

编辑:我删除了大部分不需要的括号。如果我还有一些,请告诉我。这就是我所拥有的,它几乎完美地运行和编译。

import java.util.Scanner;

public class XOBYT
{

    public static void main(String[] args)
    {
       String choice;
       Scanner scan = new Scanner(System.in);

       System.out.println("COPYRIGHT 2016 HARBNGER STUDIOSYou wake up in a broken-down alabaster\nhouse with no knowladge of who\nyou are or where you came from.\nWhich direction do you want to head? (options: NORTH, SOUTH, EAST, WEST)");

       choice = scan.nextLine();
       if(choice.equals("NORTH")){
               System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED NORTH TO A RUN-DOWN VILLAGE. CARCASSES EVERYWHERE.\nNO SURVIVORS. I WONDER WHAT HAPPENED HERE.\nI LOOTED THE BODIES, TALOS BLESS MY SOUL. I FOUND A PISTOL AND SOME GAUZE. I CAN VIEW MY INVENTORY, HIT THE ROAD, OR LINGER AROUND.\n\n(options: INVENTORY,ROAD,LINGER)");
       }

       choice = scan.nextLine();
       if(choice.equals("INVENTORY")){
            System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644\n\nI TRAVELED SOUTH AND STOPPED AT AN ABANDONED\nOUTPOST. IT HAS SOME SUPPLIES. I PICKED\nUP A PISTOL AND SOME NANITE SYRINGES. I CAN CHECK\nMY INVENTORY, MOVE FARTHER SOUTH, OR EXPLORE.\n\n(options: INVENTORY,FARTHER,EXPLORE)");
       }

       choice = scan.nextLine();
       if(choice.equals("FARTHER")){
            System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED EAST AND STOPPED BY A DIRTY STREAM.\nIT'S FILLED WITH GARBAGE, BUT THERE ARE ALSO PLASTIC BOTTLES.\nSHOULD I FILL THEM WITH WATER OR MOVE ON?\n\n(options: FILL,MOVE)");
       }

       choice = scan.nextLine();
       if(choice.equals("EXPLORE")){
            System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED WEST AND FOUND AN ABANDONED SHACK.\nSHOULD I SEARCH IT FOR LOOT OR MOVE ON?\n\n(options: SEARCH,MOVE)");
       }

       choice = scan.nextLine();
       if(choice.equals("SOUTH")){
            System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644\n\nI TRAVELED SOUTH AND STOPPED AT AN ABANDONED\nOUTPOST. IT HAS SOME SUPPLIES. I PICKED\nUP A PISTOL AND SOME NANITE SYRINGES. I CAN CHECK\nMY INVENTORY, MOVE FARTHER SOUTH, OR EXPLORE.\n\n(options: INVENTORY,FARTHER,EXPLORE)");
       }

       choice = scan.nextLine();
       if(choice.equals("EAST")){
            System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED EAST AND STOPPED BY A DIRTY STREAM.\nIT'S FILLED WITH GARBAGE, BUT THERE ARE ALSO PLASTIC BOTTLES.\nSHOULD I FILL THEM WITH WATER OR MOVE ON?\n\n(options: FILL,MOVE)");
       }

       choice = scan.nextLine();
       if(choice.equals("WEST")){
            System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED WEST AND FOUND AN ABANDONED SHACK.\nSHOULD I SEARCH IT FOR LOOT OR MOVE ON?\n\n(options: SEARCH,MOVE)");}
       }

       choice = scan.nextLine();
    }
}

测试。完美地工作。

2 个答案:

答案 0 :(得分:1)

看起来你来自Basic / Pascal等程序语言。

要允许扩展和更多房间,您应该将功能分解为类。

位置 - 详细说明玩家所在的位置

退出 - 从房间有效退出(添加到该位置的退出列表)

项目 - 房间或库存中的任何项目

命令 - 可能是退出的超级类,详细说明可以用某些东西做什么。

这只是一个开始的想法。然后,您可以拥有一个包含在开始时加载的房间等的文件,这样您就可以使用一个引擎来制作多个RPG级别/冒险。 (你甚至可以链接图形和声音)

如果你想看看像TinyMuck(C / C ++)这样的东西是怎么做的,你可能会有更好的想法 - 那里有一个Java版本的MUD / MUCK,它主要面向对象。

另一方面,这是代码的清理版本

    import java.util.Scanner;

public class XOBYT
{

    public static void main(String[] args)
    {
        String choice;
        Scanner scan = new Scanner(System.in);
        System.out.println("COPYRIGHT 2016 HARBNGER STUDIOS\nYou wake up in a broken-down alabaster\nhouse with no knowledge of who\nyou are or where you came from.\nWhich direction do you want to head? (options: NORTH, SOUTH, EAST, WEST)");
        choice = scan.nextLine();
        if(choice.equals("NORTH")){System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED NORTH TO A RUN-DOWN VILLAGE. CARCASSES EVERYWHERE.\nNO SURVIVORS. I WONDER WHAT HAPPENED HERE.\nI LOOTED THE BODIES, TALOS BLESS MY SOUL. I FOUND A PISTOL AND SOME GAUZE. I CAN VIEW MY INVENTORY, HIT THE ROAD, OR LINGER AROUND.\n\n(options: INVENTORY,ROAD,LINGER)");}
            choice = scan.nextLine();
        if(choice.equals("INVENTORY")){
        System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644\n\nI TRAVELED SOUTH AND STOPPED AT AN ABANDONED\nOUTPOST. IT HAS SOME SUPPLIES. I PICKED\nUP A PISTOL AND SOME NANITE SYRINGES. I CAN CHECK\nMY INVENTORY, MOVE FARTHER SOUTH, OR EXPLORE.\n\n(options: INVENTORY,FARTHER,EXPLORE)");}
        choice = scan.nextLine();
        if(choice.equals("FARTHER")){
        System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED EAST AND STOPPED BY A DIRTY STREAM.\nIT'S FILLED WITH GARBAGE, BUT THERE ARE ALSO PLASTIC BOTTLES.\nSHOULD I FILL THEM WITH WATER OR MOVE ON?\n\n(options: FILL,MOVE)");}
        choice = scan.nextLine();
        if(choice.equals("EXPLORE")){
        System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED WEST AND FOUND AN ABANDONED SHACK.\nSHOULD I SEARCH IT FOR LOOT OR MOVE ON?\n\n(options: SEARCH,MOVE)");}}
        choice = scan.nextLine();

        if(choice.equals("SOUTH")){
            System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644\n\nI TRAVELED SOUTH AND STOPPED AT AN ABANDONED\nOUTPOST. IT HAS SOME SUPPLIES. I PICKED\nUP A PISTOL AND SOME NANITE SYRINGES. I CAN CHECK\nMY INVENTORY, MOVE FARTHER SOUTH, OR EXPLORE.\n\n(options: INVENTORY,FARTHER,EXPLORE)");}
        choice = scan.nextLine();
        if(choice.equals("EAST")){
            System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED EAST AND STOPPED BY A DIRTY STREAM.\nIT'S FILLED WITH GARBAGE, BUT THERE ARE ALSO PLASTIC BOTTLES.\nSHOULD I FILL THEM WITH WATER OR MOVE ON?\n\n(options: FILL,MOVE)");}
        choice = scan.nextLine();
        if(choice.equals("WEST")){
            System.out.println("\n\n\n\n--TERMINAL_LOG--\n\nI FOUND THIS HANDHELD TERMINAL LOG FIGURED I'D\nPUT IT TO SOME USE. TODAY IS, ACCORDING TO THIS, THE 15TH OF JAN, 2644.\n\nI TRAVELED WEST AND FOUND AN ABANDONED SHACK.\nSHOULD I SEARCH IT FOR LOOT OR MOVE ON?\n\n(options: SEARCH,MOVE)");}}
       choice = scan.nextLine();
    }

顺便说一下,没有任何循环可以循环来循环执行命令。

答案 1 :(得分:0)

我很抱歉我的错误回答。我再看了一眼,我不认为那些if语句是嵌套的。 NORTH if语句在print语句之后立即结束。因此,如果你不说北方,它就不会跳过与询问行动有关的if语句。

此外,我认为您的问题是,当您不说库存时,会发生什么,它不会崩溃。它实际上只是要求另一个输入。但由于没有提示,看起来它没有做任何事情。

我认为代码看起来应该是这样的:(你应该根据自己的目的进行编辑)

public static void main(String[] args){
  String choice;
  Scanner scan = new Scanner(System.in);
  System.out.println("text");
  choice = scan.nextLine();
  if(choice.equals("NORTH")){
    System.out.println("text");
    choice = scan.nextLine();
    if(choice.equals("INVENTORY")){
      System.out.println("text");
    }

    if(choice.equals("FARTHER")){
      System.out.println("text");
    }

    if(choice.equals("EXPLORE")){
      System.out.println("text");
    }
  }
}