gpt4 book ai didi

java - 多个随机数结果

转载 作者:行者123 更新时间:2023-12-01 18:35:00 26 4
gpt4 key购买 nike

目前在 Android Studio 中工作并遇到了一些麻烦。试图让我的 rollScore 按钮不断滚动。此时它“滚动”一次并停止。我尝试过 for 循环和 while 循环,但无法让它允许多个“滚动”。

public class GameScreen extends AppCompatActivity {

private Button rollButton; // Roll button being declared as a variable
private TextView rollScore; // Text view being declared as a variable
private TextView totalSCore; //
private int mCounter = 0;
private int totalRuns = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);

Random randomGenerator = new Random();
final int randomInt = randomGenerator.nextInt(7) + 1;

rollButton = (Button) findViewById(R.id.rollButton);
rollScore = (TextView) findViewById(R.id.rollScore);
totalSCore = (TextView) findViewById(R.id.totalScore);



rollButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rollScore.setText(Integer.toString(randomInt));
mCounter++;

}
});

}
}

最佳答案

每次点击按钮时,您都必须生成一个新的随机数。

像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);

Random randomGenerator = new Random();
int randomInt;

rollButton = (Button) findViewById(R.id.rollButton);
rollScore = (TextView) findViewById(R.id.rollScore);
totalSCore = (TextView) findViewById(R.id.totalScore);



rollButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

randomInt = randomGenerator.nextInt(7) + 1;
rollScore.setText(Integer.toString(randomInt));
mCounter++;

}
});

}

关于java - 多个随机数结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60078249/

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