java通过数组随机化坐标
我一直试图在点击按钮时将按钮传送到屏幕上的一个随机位置(在9个给定坐标中的一个),效果很好,但我发现一个常见的结果是按钮保持在原位,然后我创建了一个新数组,其中不包括按钮的当前坐标,这在按钮不在同一位置的方面起了作用,但是,现在我遇到了一个新问题,出于一些未知的原因-是的,我尝试调试-按钮只指向2个坐标,下面是代码:
gameBtnRX = gameBtn.getX(); //Getting the current X of the button
gameBtnRY = gameBtn.getY(); //Getting the current Y of the button
switch ((int) gameBtnRX) { //Setting the _randArgsX array values
case 200: _randArgsX = new float[]{600, 1000}; break;
case 600: _randArgsX = new float[]{200, 1000}; break;
case 1000: _randArgsX = new float[]{200, 600}; break;
default: _randArgsX = new float[]{200, 600, 1000}; break;
}
switch ((int) gameBtnRY) { //Setting the _randArgsY array values
case 500: _randArgsY = new float[]{1000, 1500}; break;
case 1000: _randArgsY = new float[]{500, 1500}; break;
case 1500: _randArgsY = new float[]{500, 1000}; break;
default: _randArgsY = new float[]{500, 1000, 1500}; break;
}
randGameBtnX = rand.nextInt(_randArgsX.length - 1); //Randomizing one of (max 3) the values in the array
randGameBtnY = rand.nextInt(_randArgsY.length - 1); //And putting them as the X and Y of gameBtn
gameBtnX = _randArgsX[randGameBtnX];
gameBtnY = _randArgsY[randGameBtnY];
gameBtn.setX(gameBtnX);
gameBtn.setY(gameBtnY);
(200600100是可能的X坐标,500100015000是可能的Y坐标)
# 1 楼答案
见Random.nextInt(int)
因此,不需要为数组的长度减去1