0

プログラムは、main で while ループ メニューを使用して、ユーザー コマンドを要求します。

public static void main(String[] args)throws Exception
{
    Boolean meow = true;

    while(meow)
    {
        System.out.println("\n  1. Show all records.\n"
            + "  2. Delete the current record.\n"
            + "  3. Change the first name in the current record.\n"
            + "  4. Change the last name in the current record.\n"
            + "  5. Add a new record.\n"
            + "  6. Change the phone number in the current record.\n"
            + "  7. Add a deposit to the current balance in the current record.\n"
            + "  8. Make a withdrawal from the current record if sufficient funds are available.\n"
            + "  9. Select a record from the record list to become the current record.\n"
            + " 10. Quit.\n");
        System.out.println("Enter a command from the list above (q to quit): ");
        answer = scan.nextLine();
        cmd.command(answer);
        if(answer.equalsIgnoreCase("10") || answer.equalsIgnoreCase("q"))
        {
            meow = false;
        }

    }
}

選択したコマンドが実際にはメニューのコマンドではない場合、次のことが起こります。

else
        {
            System.out.println("Illegal command");
            System.out.println("Enter a command from the list above (q to quit): ");
            answer = scan.nextLine();
            command(answer);
        }

新しい人物を追加したり、値の入力を終了するために Return キーを押す必要のあるコマンドを使用したりするたびに、else ステートメントが表示され、次に通常のコマンド リクエストが表示されます。

したがって、次のようになります。

Enter a command from the list above (q to quit): 
Illegal command
Enter a command from the list above (q to quit): 

これが起こるとき。

ここに私の完全なコードを投稿するつもりはありません。代わりにそれらのペーストビンを用意してください。

なぜこれが起こっているのか知っている人はいますか?

4

2 に答える 2

1

問題は、次のようなものScanner::nextDoubleが改行文字を読み取らないため、次Scanner::nextLineが空行を返すことです。

の出現箇所をすべて置き換えるScanner::nextLineScanner::next修正されます。

Scanner::nextLine最後の非next メソッドの後に実行することもできますnextLineが、これは少し面倒です。

私がお勧めする他のいくつかのこと:

  1. プログラムの先頭に追加scan.useDelimiter("\n");し、行にスペースを追加してテストすると、なぜこれが必要なのかがわかります。

  2. に変更printlnしてprint、コマンドを同じ行に入力できるようにします。すなわち:

    変化する

    System.out.println("Enter a command from the list above (q to quit): ");`
    

    System.out.print("Enter a command from the list above (q to quit): ");
    
  3. これを変える:

    else
    {
        System.out.println("Illegal command");
        System.out.println("Enter a command from the list above (q to quit): ");
        answer = scan.nextLine();
        command(answer);
    }
    

    に:

    else System.out.println("Illegal command");
    

    メニューをもう一度印刷しますが、不要な再帰を避けることができます。メニューを再度印刷しないようにするのは簡単です。

  4. 実行する前に終了を確認するcommandことをお勧めします (その後、そのチェックインを削除できますcommand)。

    System.out.println("Enter a command from the list above (q to quit): ");
    answer = scan.nextLine();
    if (answer.equalsIgnoreCase("10") || answer.equalsIgnoreCase("q"))
        meow = false;
    else
        cmd.command(answer);
    
  5. に変更Booleanbooleanます。この場合は不要なBooleanのラッパー クラスです。boolean

于 2013-03-28T10:46:34.683 に答える
0

たぶん\n、while ループの最後で、再び入力を取得する直前にバッファに残ります。多分

while(meow)

{
  scan.nextLine();

これはそれを取り除くのに役立ちます。

于 2013-03-28T10:44:40.990 に答える