1

基本的に1行の文字列を返すプログラムが1つありますが(これは正常に機能します)、複数行になることもあり、問題が発生します。基本的に、2番目のプログラムに送信されるオプションをユーザーに選択させ、そのオプションに基づいて特定の文字列を返します。ただし、ユーザー選択をループの外に置くと、返される文字列が複数行の場合は正常に機能しますが、ユーザーが終了することを選択するまで、オプションメニューでユーザーにプロンプ​​トを表示し続ける必要があります。そのためのコードは次のとおりです。

System.out.print("Enter your choice: ");
fromClient = stdIn.readLine().trim();

while ((fromServer = input.readLine()) != null)
    {
        System.out.println("Server: " + fromServer);            
        if (fromServer.equals("Bye"))
        break;          


        if(fromClient.equals("1"))
        {
            System.out.println("Client: " + fromClient);
            output.println(fromClient);

        }
        if(fromClient.equals("2"))
        {
            System.out.println("Client: " + fromClient);
            output.println(fromClient);

        }
        if(fromClient.equals("3"))
        {
            System.out.println("Client: " + fromClient);
            output.println(fromClient);

        }
        if(fromClient.equals("4"))
        {
            System.out.println("Client: " + fromClient);
            output.println(fromClient);
            break;

        }


    }

これは、前述のとおり、複数行の文字列を正常に返しますが、オプションの選択をユーザーに求めるプロンプトは表示されなくなります。ユーザー選択をループ内に移動し、複数行の文字列が返されると、1行を出力し、ユーザーに選択を求め、2行目を印刷し、ユーザーに選択を求め、3行を印刷し、ユーザーに選択を求めます...

4

2 に答える 2

1

コードに従うのはかなり難しいです。より多くのコンテキストが役立ちます。「インプット」とは?(つまり、fromServer = input.readLine())

とにかく、サーバーが送信する行数に関係なく、ユーザー入力を求める前に一度に 1 行しか読み取っていません。クライアントのコマンドを送信する前に、サーバーの応答をすべて処理してみてください。例えば:

boolean shouldExit = false;

while ((fromServer = input.readLine()) != null){
    System.out.println("Server: " + fromServer);                     
    if (fromServer.equals("Bye")){
        shoudExit = true;
        break;
    }
}

if (shouldExit) System.exit(0)  // Or whatever you want to do

//Now handle sending your client's command to the server.
//...
于 2012-09-11T17:25:43.560 に答える
0

クライアントとサーバーを複数回通信する場合は、2 つのループを作成する必要があります。

  1. 最初のループはユーザーに選択を求め、2 番目のループを起動し、サーバーの応答とユーザーの選択に基づいて必要なことを行います
  2. 2 番目のループは、サーバーからすべての行を読み取ります

次のようになります。

System.out.print("Enter your choice: ");
while( true ) {
    fromClient = stdIn.readLine().trim();

    StringBuilder responseSb = new StringBuilder();
    while( ( fromServer = input.readLine() ) != null ) {
        responseSb.append( fromServer );
        responseSb.append( "\n" );
    }

    String response = responseSb.toString().trim();

    System.out.println( "Server: " + response );
    if( response.equals("Bye") ) {
        break;
    }


    if(fromClient.equals("1"))
    {
        System.out.println("Client: " + fromClient);
        output.println(fromClient);

    }
    if(fromClient.equals("2"))
    {
        System.out.println("Client: " + fromClient);
        output.println(fromClient);

    }
    if(fromClient.equals("3"))
    {
        System.out.println("Client: " + fromClient);
        output.println(fromClient);

    }
    if(fromClient.equals("4"))
    {
        System.out.println("Client: " + fromClient);
        output.println(fromClient);
        break;

    }


}
于 2012-09-11T18:03:06.097 に答える