2011-07-29 66 views
0

所以我有一個while循環被觸發,並命名我正在使用一個額外的值。什麼導致++值倒退?

totalPlayerCount可以是1,2,3或4,無論我選擇什麼似乎自動設置nameLoop該值..無論nameLoop = 1;

最後,我有nameLoop ++;作爲底部,當它開始說它播放器4,然後播放器3,然後播放器2,然後播放器1 ...

爲什麼地球上它會倒退? Theres在整個文件中只有3個nameLoop實例,所以它在其他地方不受影響。

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

莫非它使他們以正確的順序,但4由最後所以會出現上的3頂部,這是對的2頂部,這是對的1頂部?

編輯:如何像

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

相關問題