0

ユーザーがコマンドラインで終了するまでプレイするゲームを作成しています。

ユーザーは、get や go などのさまざまなコマンドを入力できます。get コマンドを使用すると、ユーザーは何を取得するか、get 野球のバットを取得することができます。私のコードで行うことは、コマンドを分割することです。

すべて正常に動作していますが、解決できないバグが見つかりました。「get」と入力して押してから+を押すspaceと、終了しない while ループに入ります。ctrlz

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 に答える