是什么导致了++价值倒退?

发布于 2024-11-27 04:59:35 字数 1325 浏览 0 评论 0原文

所以我有一个被触发的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言 2024-12-04 04:59:35

是否它按正确的顺序排列它们,但 4 是最后排列的,因此它出现在 3 的顶部,3 位于 2 的顶部,而 3 又位于 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;
}

它只是在我的脑海中,未经测试!

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

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;
}

It's just off the top of my head and not tested!

谁把谁当真 2024-12-04 04:59:35

看起来您在迭代玩家的同时正在倒计时玩家的数量。尝试将其更改为:

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

这应该可以解决问题,但是您真正需要更改的部分是您使用nameLoop分配玩家编号的位置。

It looks like you are counting down the number of players while iterating through them. Try changing it to:

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

This should fix it, but the part you really need to change is where ever you are using nameLoop to assign player number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文