是什么导致了++价值倒退?
所以我有一个被触发的 while 循环,并且为了命名我使用了一个附加值。
TotalPlayerCount 可以是 1,2,3 或 4,无论我选择什么,似乎都会自动将 nameLoop 设置为该值.. 无论 nameLoop = 1;
最重要的是,我有 nameLoop++;作为底部,当它开始时,它会显示“玩家 4”,然后是“玩家 3”,然后是“玩家 2”,然后是“玩家 1”……
到底为什么它会倒退?整个文件中只有 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;
}
So I have a while loop that is being triggered, and for naming I am using an additional value.
totalPlayerCount can be either 1,2,3 or 4 and whatever I choose for that seems to automatically set nameLoop that value.. regardless of nameLoop = 1;
To top it off, I have nameLoop++; as the bottom and when it starts it says Player 4, then Player 3, then Player 2, then Player 1...
Why on earth would it be going backwards? Theres only 3 instances of nameLoop in the whole file so its not being affected anywhere else.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是否它按正确的顺序排列它们,但 4 是最后排列的,因此它出现在 3 的顶部,3 位于 2 的顶部,而 3 又位于 1 的顶部?
编辑:像这样的东西怎么样?
它只是在我的脑海中,未经测试!
Could it be that it makes them in the right order, but 4 is made last so it appears on top of 3, which is on top of 2, which is on top of 1?
EDIT: How about something like
It's just off the top of my head and not tested!
看起来您在迭代玩家的同时正在倒计时玩家的数量。尝试将其更改为:
这应该可以解决问题,但是您真正需要更改的部分是您使用
nameLoop
分配玩家编号的位置。It looks like you are counting down the number of players while iterating through them. Try changing it to:
This should fix it, but the part you really need to change is where ever you are using
nameLoop
to assign player number.