Random text app show same text 2 times or more in a row

问题: I made a simple app that shows you a random text when you click a button. It all works fine but sometimes it shows the same text 2 or 3 times in a row. I know I can fix thi...

问题:

I made a simple app that shows you a random text when you click a button. It all works fine but sometimes it shows the same text 2 or 3 times in a row. I know I can fix this with if statement, but I don't know exactly how to do that. Here's my code.

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]);             
        }
    });
  }
}

回答1:

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

回答2:

You know it's random so it is normal to get the same value few times in a row. But if you want to prevent it you can just save previous index.

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]);             
        }
   });
}
}

回答3:

You can just save your last string that you randomly generated in a variable and check if the new text is the same:

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
}
  • 发表于 2019-03-16 12:20
  • 阅读 ( 157 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除