我尝试更改字符串数组中的随机索引,并将其放入JPanel
g.drawString(Str, int, int)
。
我无法将数组的特定随机索引传递到Str槽。
String[] messages = {"WOW, these fireworks are super cool.", "that one looks like a dragon", "did a star just explode","This would be cool on fourth of July.", "We should have packed a picnic"};
g.drawString(Arrays.toString(messages), getWidth()-300, (getHeight()/2)-200);
截至目前,这将打印整个数组,我只想要一个特定的随机索引。我在考虑使用这样的东西:
i=rand.nextInt(4);
messages[i];
我只想打印messages[i]
答案 0 :(得分:1)
试试这个:
int i=new Random().nextInt(4);
String[] array={"Hello","Random Text","Apple","3.14159","Bye"};
g.drawString(array[i],300, 200);
希望这会有所帮助。
答案 1 :(得分:0)
应该是这样的:
String[] stringsData = new String[5];
[...]
Random random = new Random();
String randomString = stringsData[random.nextInt( stringsData.length )];
g.drawString(randomString, 0, 0);