-1
我有这个代码,我想要随机返回字符串,因此每次都会返回一个不同的字符串。它的工作原理罚款Math.random()没有正确返回
words = sentenceStore[rnd.nextInt(sentenceStore.length)];
,但试图利用数学类的方法,它只是返回每次
words = sentenceStore[(int)Math.random() * sentenceStore.length];
谁能帮助索引0的字符串?
WordBank.java(无主此方法):
import java.util.Random;
public class WordBank {
private String [] sentenceStore;
public String getSentence (String words) {
String [] sentenceStore = new String [5]; //i'll store 5 sentences here for now.
sentenceStore[0] = "Hello how are you doing";
sentenceStore[1] = "What do you want eh";
sentenceStore[2] = "Hello can i help you";
sentenceStore[3] = "You are so incredibly pretty";
sentenceStore[4] = "What was that you said";
Random rnd = new Random();
words = sentenceStore[rnd.nextInt(sentenceStore.length)];
//words = sentenceStore[(int)Math.random() * sentenceStore.length];
return words;
}
}
优秀。非常感谢你。 – user3814983
不用客气 – Phash