0

我正在制作一个游戏,直到用户在命令行中输入退出。

用户可以输入不同的命令,例如 get 和 go,使用 get 命令用户可以说出要获得的内容,获得棒球棒。我在代码中所做的是拆分命令。

一切正常,但我发现了一个我无法解决的错误。如果我输入“get”并按space然后ctrl+z它会进入一个永远不会结束的while循环。

它只发生在ctrl+ z(1 次,ctrl c但之后 1 次不再)

 private void run() 
{       
    while (! quitCommand)
    {
        
        String input = null;
        
        try 
        {   
            input = null;
            System.out.println("Input "+ input);
            System.out.println("Give a command.");
            BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
            input = is.readLine();
            handleCommand(input);
            // As long as the command isn’t to quit:
            // get the next input line and handle it. (With handleCommand.)
        } 
        
        catch (Exception e) 
        {
            System.out.println("Something went wrong we are sorry try again.");
            e.printStackTrace();
        }
        
    }
}

/**
* @param userInput (This is the entire input string from the user.)
*
* (Tell others to) Perform the task which belongs to the given
* command.
*/
private void handleCommand(String userInput) 
{
    
    
    // Split the user input string.
    if (userInput != null)          // user input can not be empty
    {
        String[] delenTekst = userInput.split(" ");
        
        // The first word is a command. The rest is extra information
        String command = delenTekst[0];
        String extra = "";
        
        for (int i = 1; i < delenTekst.length; i ++)
        {
            if (i == 1)
            {
                extra = extra + delenTekst[i];
            }
            else
            {
                extra = extra +" " + delenTekst[i];
            }               
        }

        switch (command)
        {
            // Check if the command is to travel between rooms. If so, handle
            case "go"
            : 
                this.checkRoomTravel(extra);
                break;
            // If there isn't any room travel, then check all other command
            case "get"
            :   
                System.out.println("Looking for " +extra );
                this.handleGetCommand(extra);
                break;
            case "quit"
            :
                quitCommand = true;
                break;
            default
            :
                System.out.println("Command is not known try help for information");
                break;  
        }
    }
    else
    {
        userInput = "help";
    }
}

我是 Java 新手,所以它可能非常简单。

在我的脚本顶部,我有一个私有布尔值quitCommand = false;,用于检查用户是否输入了退出。

4

2 回答 2

1

Ctrl+Z关闭控制台,因此您的readLine()返回 null 假装表明已到达文件末尾。因此,您需要做的就是在处理“退出”时检查null返回readLine()并处理此问题。

我已经更改了您的代码(只是为了测试我的论文)并且还简化了一些内容,例如您不需要在BufferedReader每次阅读一行时重新创建一个。

private boolean quitCommand = false;
 private void runIt() {       
     BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
     String input = null;

     while(!quitCommand) {
         try {   
             System.out.print("Give a command: ");
             input = is.readLine();

             // As long as the command isn’t to quit:
             if(input == null || "quit".equals(input.trim())) quitCommand = true;
             if(quitCommand) break;

             // get the next input line and handle it. (With handleCommand.)
             String[] words = input.trim().split("\\s+");

             // ** This is the original handleCommand line **
             System.out.println(input + ":" + Arrays.toString(words));
         } 
         catch (Exception e) {
             System.out.println("Something went wrong we are sorry try again.");
             e.printStackTrace();
         }
     }
 }

顺便说一句:要将输入拆分为单词,我会使用代码中显示的正则表达式。如果用户输入制表符或多个空格,这也有效。

于 2013-10-19T15:27:11.517 回答
0

在 DOS/Windows Ctrl+Z表示输入结束。null无论您调用多少次,这都会导致 readLine() 返回。这可能会导致您的代码失败,因为您似乎没有检查它。我怀疑你得到了一个 NullPointerException ,你假装没有发生并无休止地再次尝试。

于 2013-10-19T15:22:45.273 回答