爲什麼你得到了相同的答案5倍的原因是因爲字符串您do-while循環運行5次,在不改變「生物」。
System.out.println(a +" "+ b + " " + c + " " + d + " " +e);
如果刪除do-while循環,你會得到相同的答案只有一次但是以防萬一,我誤解你的問題我做了在其中獲得了多個隨機結果的簡單的方法一個簡單演示for循環,字符串數組和隨機類
String[] creatures = {"Dog", "Cat", "Fish", "Monkey", "Horse"};
Random r = new Random();
for (int i = 0; i < 5; i++) {
String creature1 = creatures[r.nextInt(creatures.length)];
String creature2 = creatures[r.nextInt(creatures.length)];
String creature3 = creatures[r.nextInt(creatures.length)];
String creature4 = creatures[r.nextInt(creatures.length)];
String creature5 = creatures[r.nextInt(creatures.length)];
System.out.println(creature1 + " " + creature2 + " " + creature3
+ " " + creature4 + " " + creature5);
}
您不會將值更改爲循環內的'a','b','c','d'或'e'。所以我不明白你爲什麼期望價值觀發生變化。 –