我正在开发一个单词scramble android app ..所以基本上我有一个字符串数组的单词。我需要显示字符串而不重复。这是我到目前为止所尝试的:
// shuffle algorithm
private String shuffleWord(String word){
List<String> letters = Arrays.asList(word.split(""));
Collections.shuffle(letters);
String Shuffled="";
for (String letter : letters ){
Shuffled += letter;
}
return Shuffled;
}
private void newGame(){
// get random word from dictionary
currentWord= dictionary[r.nextInt(dictionary.length)];
// show the shuffled word
tv_word.setText(shuffleWord(currentWord));
// clear the textfield
et_guess.setText("");
// switch buttons
b_new.setEnabled(false);
b_check.setEnabled(true);
}
答案 0 :(得分:0)
您需要使用不同的种子来每次获得不同的伪随机数,这可用于随机化您的单词。像这样:
Random random = new Random(System.currentTimeMillis());
currentWord= dictionary[random.nextInt(dictionary.length)];
请阅读https://developer.android.com/reference/java/util/Random.html#Random(long)