2011-07-29 3 views
0

私はトリガされているwhileループを持っており、命名のために追加の値を使用しています。++の値を後ろに移動させる原因は何ですか?

totalPlayerCountは1,2,3,4のどちらでもかまいません。私が選択するものは、nameLoopを自動的に設定するようです。nameLoop = 1;

私はnameLoop ++を持っています。最後にプレイヤー4、次にプレイヤー3、プレイヤー2、プレイヤー1、プレイヤー1と表記します。

なぜ地球上で逆転するのでしょうか? Theresはファイル全体の中でnameLoopの3つのインスタンスしかないので、他の場所には影響しません。

public void setNames() { 
    int nameLoop = 1; 
    while (totalPlayerCount >= 1){   

    //**********************// 
    //***SET PLAYER NAMES***// 
    //**********************// 
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 

    alert.setTitle("Player " + nameLoop); 
    alert.setMessage("Name:"); 

    // Set an EditText view to get user input 
    final EditText input = new EditText(this); 
    alert.setView(input); 

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     name = input.getText().toString(); 
     // Do something with value! 
     Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
     // Canceled. 
     } 
    }); 

    alert.show(); 
    totalPlayerCount--; 
    nameLoop++; 
    } 
    return; 
} 

答えて

0

プレイヤーの数をカウントしているうちに、反復しているようです。それを変更してみてください:

int nameLoop = totalPlayerCount; //the 2nd line of your code 
... 
totalPlayerCount--; //the bottom few lines of your code 
nameLoop--; 

これは、それを修正する必要がありますが、今まであなたがプレーヤーの番号を割り当てることnameLoopを使用しているどこ本当にを変更する必要がある部分があります。

+0

あなたは、それが後方に数えていなかった、あなたはそれらを後方にループしている(またはそれらの名前を後方に割り当てる)ことを見ています。 – Amplify91

+0

私は、トリックをした、後で持って..一度にすべての4を作成することを防ぐための方法はありますか? 1を作成して、もう一度ループし、再び作成してループします。 – Rob

2

はそれが正しい順序でそれらを作るということでしたが、それは3の最上部に表示されますので、4は1の上にある、2の上にある、最後に作られるのですか?

EDIT:どのように

private int nameLoop = 1; // make nameLoop a data member 

public void setNames(int nameLoop) { 
    if(nameLoop<totalPlayerCount) { 
     //Build and show dialog here 
     public void onClick(... ...) { 
      nameLoop++ 
      setNames(nameLoop); 
     } 
    } 
    return; 
} 

のようなものについて、それはちょうど私の頭の上からだとテストされていません!

+0

そうかもしれないと思います。私はそれを一度に1つしか起こらないようにする方法を見つけ出す必要があるように見えます。 – Rob

関連する問題