Java I';我在做一个文字游戏,有些选项被忽略了,但我没有出错

Java I';我在做一个文字游戏,有些选项被忽略了,但我没有出错,java,Java,所以我开始做这个Java文本游戏,有几个选项不会做任何事情,只是结束程序。到目前为止,我注意到的是记者2离开枪口,正朝着走廊走去。代码太大了,很抱歉,我是Java新手,它会非常混乱,但我真的需要帮助。我已经为此工作了几个小时,只是注意到这些部分不起作用,我很困惑:/无论如何,这里是代码(很抱歉格式化错误,我不擅长在这里格式化) 这段代码将极难维护、调试和更改。 我建议重构它并给它一点结构。即: if (actionOne == 1) { System.out.printl

所以我开始做这个Java文本游戏,有几个选项不会做任何事情,只是结束程序。到目前为止,我注意到的是记者2离开枪口,正朝着走廊走去。代码太大了,很抱歉,我是Java新手,它会非常混乱,但我真的需要帮助。我已经为此工作了几个小时,只是注意到这些部分不起作用,我很困惑:/无论如何,这里是代码(很抱歉格式化错误,我不擅长在这里格式化)


这段代码将极难维护、调试和更改。 我建议重构它并给它一点结构。即:

    if (actionOne == 1) {
        System.out.println(getActionMessage1());
        actionTwo = keyboard.nextInt();
        if (actionTwo == 1) {
            System.out.println(getActionMessage1_1());
        } else if (actionTwo == 2) {
            System.out.println(getActionMessage1_2());
        }
    } else ...

public String getActionMessage1() {
    return "The long string now is not making your logic with decisions hard to understand" +
           "and it's easy to change the messages without affectiong your code logic"
}
当然,这里可以考虑很多事情来进行更聪明的设计,但这第一步会让设计更简单

对于缩进问题,您可以使用:

一旦您解决了这个问题,您将能够立即修复代码

更新

你的第三幕和第四幕没有对齐,第八幕和第九幕也没有对齐。请尝试使用此代码。我懒得做我建议的重构。。。但是,如果您打算更改代码,我强烈建议您使用它

import java.util.*;

public class Spookster

{
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System. in );
        int actionOne, actionTwo, actionThree, actionFour, actionFive, actionSix, actionSeven, actionEight, actionNine;

        System.out.println("You wake up in a metallic room. There is a knife next to you, and there is a door ahead of you.");
        System.out.print("Enter 1 to go to the door, enter 2 to pick up the knife: ");
        actionOne = keyboard.nextInt();

        if (actionOne == 1) {
            System.out.println(" ");
            System.out.println("You walk to the door. There is a small window that you could look through.");
            System.out.println("You could also just open the door right there.");
            System.out.print("Enter 1 to look through the window, enter 2 to go through the door: ");
            actionTwo = keyboard.nextInt();
            if (actionTwo == 1) {
                System.out.println(" ");
                System.out.println("You look through the window and see a tall creature inside a cockpit.");
                System.out.println("Suddenly you are grabbed from behind and everything is black");
                System.out.println("YOU ARE DEAD");
            } else if (actionTwo == 2) {
                System.out.println(" ");
                System.out.println("You open the door and hear something behind you, you shut it fast then are in shock");
                System.out.println("Another creature is staring you down in front of you. He runs at you with a electrical sword");
                System.out.print("Enter 1 to dodge, enter 2 to defend yourself: ");
            }

        } else if (actionOne == 2) {
            System.out.println(" ");
            System.out.println("You pick up the knife and hear something behind you.");
            System.out.println("You blindly throw your hand backwards and realise you killed a tall creature.");
            System.out.println("There is an advanced gun in his hand");
            System.out.print("Enter 1 to pick up the gun, enter 2 to leave it be: ");
            actionThree = keyboard.nextInt();

            if (actionThree == 1) {
                System.out.println(" ");
                System.out.println("You grab the gun and the creature moves, you quickly aim and fire");
                System.out.println("The creature drops dead. You take a look around and notice a corridor.");
                System.out.println("You head down the corridor and it splits.");
                System.out.print("Enter 1 to head left, enter 2 to head right: ");
                actionFour = keyboard.nextInt();
                if (actionFour == 1) {
                    System.out.println(" ");
                    System.out.println("You head left. There is a control panel that controls an escape pod");
                    System.out.println("There are two buttons, a blue button and a yellow button.");
                    System.out.print("Enter 1 to press the blue button, enter 2 to press the yellow button: ");
                    actionFive = keyboard.nextInt();
                    if (actionFive == 1) {
                        System.out.println(" ");
                        System.out.println("You press the blue button. The ship begins to shake.");
                        System.out.println("You take your last breath as the ship explodes");
                        System.out.println(" ");
                        System.out.println("YOU ARE DEAD");
                    } else if (actionFive == 2) {
                        System.out.println(" ");
                        System.out.println("You press the yellow button. The escape pod door opens.");
                        System.out.println("You walk inside and hear something behind you.");
                        System.out.println("There are two switches in an alien language. One must control the door.");
                        System.out.print("Enter 1 to activate the left switch, enter 2 to activate the right switch.");
                        actionSeven = keyboard.nextInt();
                        if (actionSeven == 1) {
                            System.out.println(" ");
                            System.out.println("You activate the left switch and the hatch closes.");
                            System.out.println("There is only one other switch, you activate it.");
                            System.out.println("The escape pod launches from the ship. On the descent down you see the alien ship come towards earth.");
                            System.out.println("Seconds later the ship is blown to bits. Once you land you are greeted by a govenment agency");
                            System.out.println("They thank you for baiting the ship in, and give you a medal for your accomplishments.");
                            System.out.println(" ");
                            System.out.println(" ");
                            System.out.println("YOU SURVIVED - BAD ENDING");
                        } else if (actionSeven == 2) {
                            System.out.println(" ");
                            System.out.println("You activate the right switch and the escape pod launches into space.");
                            System.out.println("You see the alien bodies fly out of the ship before your eyes pop");
                            System.out.println(" ");
                            System.out.println("YOU ARE DEAD");
                        }
                    }
                } else if (actionFour == 2) {
                    System.out.println(" ");
                    System.out.println("You head right. There is an armory filled with alien weapons.");
                    System.out.print("Enter 1 to get an alien rifle, enter 2 to leave the weapons and continue down the hall: ");
                    actionSix = keyboard.nextInt();
                    if (actionSix == 1) {
                        System.out.println(" ");
                        System.out.println("You pick up an alien rifle. It sends off an alarm.");
                        System.out.println("Multiple aliens are rushing down the halls. You pick off a few.");
                        System.out.println("Suddenly you feel pain in your stomache and you drop to the floor.");
                        System.out.println(" ");
                        System.out.println("YOU ARE DEAD");
                    } else if (actionSix == 2) {
                        System.out.println(" ");
                        System.out.println("You decide it's not worth the risk.");
                        System.out.println("You keep heading down the corridor and find some type of needle filled with liquid.");
                        System.out.print("Enter 1 to inject the liquid into you, enter 2 to leave it: ");
                        actionEight = keyboard.nextInt();
                        if (actionEight == 1) {
                            System.out.println(" ");
                            System.out.println("You inject the liquid and see your arms and body slowly vanish.");
                            System.out.println("It seems you injected yourself with a cloaking syrum.");
                            System.out.println("Seconds later an alien comes in. It growls then moves off looking for you.");
                            System.out.println("Heading down the corridor you see another alien, this one looks different.");
                            System.out.println("It wears a cloak and crown, and looks to be of high status.");
                            System.out.print("Enter 1 to attempt to communicate, enter 2 to remain silent: ");
                            actionNine = keyboard.nextInt();
                            if (actionNine == 1) {
                                System.out.println(" ");
                                System.out.println("You speak in english 'Why am I here?!'");
                                System.out.println("To your surprise the alien responds in English");
                                System.out.println("He says 'We were testing humans, you seem like a smart one");
                                System.out.println("He says 'I would like to take you back to our home system.'");
                                System.out.println("He says 'After seeing what you can do, I believe you could help us in our galactic republic'");
                                System.out.println(" ");
                                System.out.println("1 Year Later");
                                System.out.println("You are now part of a galactic republic lightyears away. You have forgotten your old life.");
                                System.out.println("Something has made you forget it, but that doesn't matter now. You are a part of their military.");
                                System.out.println("You don't question their motives, because you have no reason to.");
                                System.out.println(" ");
                                System.out.println(" ");
                                System.out.println("YOU SURVIVED - GOOD ENDING");
                            } else if (actionNine == 2) {
                                System.out.println(" ");
                                System.out.println("You suddenly feel weak. A crunching feeling is in your body.");
                                System.out.println("It seems the alien knew you were there. He is much more powerful than the rest.");
                                System.out.println("Your insides begin to hurt until blood bursts from your stomach, you fall to the ground.");
                                System.out.println(" ");
                                System.out.println("YOU ARE DEAD");
                            }
                        } else if (actionEight == 2) {
                            System.out.println(" ");
                            System.out.println("You leave the needle there.");
                            System.out.println("Suddenly a blade pierces through you. An alien has sneaked up on you.");
                            System.out.println("As your vision fades to black, you feel absolute terror.");
                            System.out.println(" ");
                            System.out.println(" ");
                        }
                    }
                }
            } else if (actionThree == 2) {
                System.out.println(" ");
                System.out.println("The creature becomes conscious and aims the gun at you, you stand in fear.");
                System.out.println("Suddenly you hear a loud noise and everything is black.");
                System.out.println(" ");
                System.out.println("YOU ARE DEAD");
            }
        }
    }
}

与硬编码不同的是,将代码和数据按如下方式分开。我已将级别数据放在json文件levels.json中

用于解析json的库是。提取jar文件并将其放入java项目的构建路径中

import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.JSONException;
import org.json.JSONObject;

public class Main
{
    public static void main(String[] args)
    {
        String line;
        try{
            //First read in the whole file into a string
            InputStream in = new FileInputStream(new File("src/levels.json"));
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder out = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                //Reading line by line
                //Append it to String builder
                out.append(line);  
            }

            //File has been read
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Press 1 or 2:");
            int action;
            JSONObject json = new JSONObject(out.toString());
            //Loop n number of times
            //n is the depth of your game
            for(int i=0;i<2;i++){
                action = keyboard.nextInt();

                //Choose the tag depending on keypress
                String name = new String();
                switch(action){
                case 1:
                    name = new String("k1");
                    break;
                case 2:
                    name = new String("k2");
                    break;
                }           
                //Go deeper into the game
                json = json.getJSONObject(name);
                //Print the message
                System.out.println(json.getString("message"));
            }
            reader.close();
        }catch(IOException fe){
            fe.printStackTrace();           
        }catch(JSONException ee){
            ee.printStackTrace();
        }

    }
}

你的if语句括号可能搞错了。正确缩进代码,这样可以更容易地匹配右大括号和右大括号,并搜索多余的/缺少的。请尝试在此处美化您的代码:我刚刚尝试了DDan,它使代码看起来很好,但仍然无法用于以下两个部分:C我太迷路了D:这里有太多System.print语句。太乱了。我说创建一个json/xml文件来存储游戏流程。保留一个变量以读取按键,另一个变量用于存储所处的级别/操作/步骤。你明白我的建议吗?我在这里加上两条原则:方法不应该超过15行,并且在方法中只允许一个缩进。因此,我将进一步重构重构重构后的代码,将
actionTwo==keyboard.nextInt()和下面的行分成一个单独的方法。是的,正如我提到的,这将是一个更结构化的代码的第一步,也可以考虑有<代码> GeActoNeMeals[/Cord]接受参数,并从预填充的容器返回消息,因此,它只需要一个方法来检索消息。我不确定添加
getActionMessage
方法是否会提高可读性。修复缩进肯定会的。它肯定会的,因为它将决策逻辑从字符串常量中分离出来。嘿,谢谢你,这解决了所有的问题,我仍然在尝试如何用java做大部分事情,因为我只是在高中xD的计算机科学入门课上@达恩
import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.JSONException;
import org.json.JSONObject;

public class Main
{
    public static void main(String[] args)
    {
        String line;
        try{
            //First read in the whole file into a string
            InputStream in = new FileInputStream(new File("src/levels.json"));
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder out = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                //Reading line by line
                //Append it to String builder
                out.append(line);  
            }

            //File has been read
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Press 1 or 2:");
            int action;
            JSONObject json = new JSONObject(out.toString());
            //Loop n number of times
            //n is the depth of your game
            for(int i=0;i<2;i++){
                action = keyboard.nextInt();

                //Choose the tag depending on keypress
                String name = new String();
                switch(action){
                case 1:
                    name = new String("k1");
                    break;
                case 2:
                    name = new String("k2");
                    break;
                }           
                //Go deeper into the game
                json = json.getJSONObject(name);
                //Print the message
                System.out.println(json.getString("message"));
            }
            reader.close();
        }catch(IOException fe){
            fe.printStackTrace();           
        }catch(JSONException ee){
            ee.printStackTrace();
        }

    }
}
{
    "k1":{
        "message": "Enter 1 to look through the window, enter 2 to go through the door.",
        "k1": {
                "message":"YOU ARE DEAD"
            },
        "k2": {
                "message":"defend yourself."
            }
    },
    "k2":{
        "message" : "Enter 1 to pick up the gun, enter 2 to leave it be",
        "k1": {
                "message":"You grab the gun and the creature moves."
            },

        "k2":{
                "message":"The creature becomes conscious and aims the gun at you."
            }
    }
}