ランダムな事実を生成する単純な事実Javaプロジェクトがあります。 ランダムボタンをクリックすると、ランダムなファクトを表示し、そのファクト番号を探します。今ランダムな配列値を生成するJava
String factNumber[] = {
"Fact 1",
"Fact 2",
"Fact 3",
"Fact 4",
"Fact 5",
};
public String randomButtonNumber() {
return factNumber[i];
}
String facts[] = {"Elephants are the only mammals that can't jump.",
"Candles will burn longer and drip less if they are placed in the freezer a few hours before using.",
"Potatoes have more chromosomes than humans.",
"You burn more calories sleeping than you do watching television.",
"Animals that lay eggs don't have belly buttons.",
};
public String randomButton() {
Random random = new Random();
i = random.nextInt(facts.length);
return facts[random.nextInt(facts.length)];
}
、私のコードは、ランダムな事実を生成し、実際の数はこれを試してみてください1.
のですか? –
あなたは 'Random.nextInt()'を2回呼びます。一度私を保管し、もう一度事実を得るために。あなたはたぶんこれを一度だけ呼び出すべきです。 –
[配列からランダムな値を取得する](http://stackoverflow.com/questions/36825021/getting-a-random-value-from-an-array)とまったく同じです。 –