随机文字应用程式连续显示2次或以上相同文字

时间:2019-03-14 21:06:01

标签: java android

我制作了一个简单的应用,当您单击按钮时,该应用会向您显示随机文本。一切正常,但有时它连续显示2或3次相同的文本。我知道我可以使用if语句解决此问题,但我不知道该怎么做。这是我的代码。

public class MainActivity extends AppCompatActivity {

Button button;
TextView textView;

private static final String[] FACTS = {
        "McDonald’s once made bubblegum-flavored broccoli",
        "Some fungi create zombies, then control their minds",
        "The first oranges weren’t orange",
        "There’s only one letter that doesn’t appear in any U.S. state name",
        "A cow-bison hybrid is called a “beefalo”",
        "Johnny Appleseed’s fruits weren’t for eating",
        "Scotland has 421 words for “snow”",
        "The “Windy City” name has nothing to do with Chicago weather",
        "Peanuts aren’t technically nuts",
        "Samsung tests phone durability with a butt-shaped robot"

};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    textView = (TextView) findViewById(R.id.textView);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random random = new Random();
            int index = random.nextInt(FACTS.length - 0);
            textView.setText(FACTS[index]);             
        }
    });
  }
}

3 个答案:

答案 0 :(得分:2)

int index = random.nextInt(FACTS.length - 0);
while(FACTS[index].equals(textView.getText().toString()) {
    index = random.nextInt(FACTS.length - 0);
}
textView.setText(FACTS[index]);

答案 1 :(得分:1)

您知道这是随机,因此连续几次获得相同的值是正常的。但是,如果要阻止它,则可以保存以前的索引。

public class MainActivity extends AppCompatActivity {

Button button;
TextView textView;
int lastIndex = 0;

private static final String[] FACTS = {
    "McDonald’s once made bubblegum-flavored broccoli",
    "Some fungi create zombies, then control their minds",
    "The first oranges weren’t orange",
    "There’s only one letter that doesn’t appear in any U.S. state name",
    "A cow-bison hybrid is called a “beefalo”",
    "Johnny Appleseed’s fruits weren’t for eating",
    "Scotland has 421 words for “snow”",
    "The “Windy City” name has nothing to do with Chicago weather",
    "Peanuts aren’t technically nuts",
    "Samsung tests phone durability with a butt-shaped robot"

};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    textView = (TextView) findViewById(R.id.textView);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random random = new Random();
            int index;
            do {                
                index = random.nextInt(FACTS.length - 0);
            } while (index == lastIndex);
            lastIndex = index;
            textView.setText(FACTS[index]);             
        }
   });
}
}

答案 2 :(得分:0)

您只需将您随机生成的最后一个字符串保存在变量中,然后检查新文本是否相同:

String testString = "";

//now in your method that generates this random string you need to compare 
//the generated string into your string variable.
//for example in your string generating method (in your case its the button click and generated string is FACTS[index]):

if(generatedString.equals(testString)){
   //your strings are the same and you need to generate a new string
}else{
  //your strings are not the same and you are good to go
}