【发布时间】:2012-09-21 22:08:32
【问题描述】:
我对以下代码有疑问
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(4);
String wordList[] = new String[4];
{
wordList[0] = "Red";
wordList[1] = "Blue";
wordList[2] = "Green";
wordList[3] = "Orange";
}
String wordToDisplay = wordList[randomInt];
此代码工作正常,但我想知道是否有可能让它连续两次不选择同一个单词。例如,如果它只是选择了“红色”,那么它不会在下一个连续的时间再次选择“红色”。我读过一些关于 DISTINCT 的文章,但我不确定这是否正确。
这是使用此按钮的代码
final Button button1 = (Button) findViewById(R.id.button1);
final TextView textView = (TextView) findViewById(R.id.text_random_text);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(9);
String wordToDisplay = wordList[randomInt];
textView.setText(wordToDisplay);
感谢您的帮助
【问题讨论】:
-
这是在方法或循环内还是什么?
-
@IonutHulub 我有一个按钮使用字符串 String wordToDisplay = wordList[randomInt];
-
@IonutHulub 编辑了我的问题以添加更多代码
-
我说的是
int randInt = randomGenerator.nextInt(4)行。
标签: android eclipse random duplicates