gpt4 book ai didi

Java 随机化 -(减号)和 +(加号)

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:24:53 27 4
gpt4 key购买 nike

有没有办法随机化加号或减号字符?我有一个程序,其中一个 Sprite 在屏幕上移动,当你点击它时,它会重新出现在不同的地方。我也希望它移动的方向也是随机的。目前我只能将它设置为从左到右+,或者从右到左-。

private int x = random.nextInt(150); 
private int y = random.nextInt(500);
private int xSpeed = random.nextInt(10);//Horizontal increment of position (speed)
private int ySpeed = random.nextInt(10);// Vertical increment of position (speed)

public void update() {
x = x +- xSpeed;
y = y +- ySpeed;
}

最佳答案

你总是可以做一个变体:

xSpeed = xSpeed * ( random.nextBoolean() ? 1 : -1 );
ySpeed = ySpeed * ( random.nextBoolean() ? 1 : -1 );

关于Java 随机化 -(减号)和 +(加号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717033/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com