Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是什么导致a++;倒退的价值?_Android_Loops_Integer_While Loop - Fatal编程技术网

Android 是什么导致a++;倒退的价值?

Android 是什么导致a++;倒退的价值?,android,loops,integer,while-loop,Android,Loops,Integer,While Loop,所以我有一个被触发的while循环,为了命名,我使用了一个附加值 totalPlayerCount可以是1、2、3或4,我选择的任何值都会自动设置nameLoop的值。。无论名称如何,循环=1 最重要的是,我有nameLoop++;作为底部,当它开始时,它说玩家4,然后是玩家3,然后是玩家2,然后是玩家1 究竟为什么会倒退?整个文件中只有3个nameLoop实例,因此它不会在其他任何地方受到影响 public void setNames() { int nameLoop = 1;

所以我有一个被触发的while循环,为了命名,我使用了一个附加值

totalPlayerCount可以是1、2、3或4,我选择的任何值都会自动设置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;
}

这可能是因为它的顺序正确,但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;
}
private int nameLoop=1;//使nameLoop成为数据成员
公共无效集合名(int-nameLoop){

如果(nameLoop在遍历玩家时,看起来你正在倒计时。请尝试将其更改为:

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

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

我想可能就是这样。看起来我需要弄清楚如何一次只发生一个。您知道,这不是倒计时,而是倒计时(或者把他们的名字倒过来)我明白了,这就是诀窍。有没有办法防止所有4个都同时被创建?让它创建1,完成并再次循环,再次创建并循环..等等。