-2

次のプログラムでは、どの範囲の数値から 10 個の乱数を生成するかを尋ねられますが、ユーザーが選択を入力すると、表示される数値はすべて同じになりますか?

import java.io.*;

public class RandomInt
{
    public static void main (String [] args) throws IOException
    {
        String inGuess;
        int theGuessInt = 0;
        int theNum;
        int theNum2;
        BufferedReader myInput = new BufferedReader (new InputStreamReader (
                System.in));
        System.out
                .println ("Hello this program generates random numbers from 0-10 or 20-30");
        System.out
                .println ("Please input '1' for the random generation of 0-10, or '2' for the generation of 20-30");
        inGuess = myInput.readLine ();
        theGuessInt = Integer.parseInt (inGuess);
        // Generate random numbers from 0 to 10
        theNum = (int) (Math.random () * 10 - 0) + 0;
        // Generate random numbers from 20 to 30
        theNum2 = (int) (Math.random () * 30 - 20) + 20;
        if (theGuessInt == 1)
        {
            System.out
                    .println (" You chose to generate ten random numbers from 0-10");
            System.out.println (" Please wait....(Press any key to con't)");
            inGuess = myInput.readLine ();
            System.out.println (theNum);
            System.out.println (theNum);
            System.out.println (theNum);
            System.out.println (theNum);
            System.out.println (theNum);
            System.out.println (theNum);
            System.out.println (theNum);
            System.out.println (theNum);
            System.out.println (theNum);
            System.out.println (theNum);
        }
        if (theGuessInt == 2)
        {
            System.out
                    .println (" You chose to generate ten random numbers from 20-30");
            System.out.println (" Please wait....(Press any key to con't)");
            inGuess = myInput.readLine ();
            System.out.println (theNum2);
            System.out.println (theNum2);
            System.out.println (theNum2);
            System.out.println (theNum2);
            System.out.println (theNum2);
            System.out.println (theNum2);
            System.out.println (theNum2);
            System.out.println (theNum2);
            System.out.println (theNum2);
            System.out.println (theNum2);
        }
    }
}
4

2 に答える 2

6

さて、あなたはやっています:

System.out.println(theNum);
System.out.println(theNum);
System.out.println(theNum);
System.out.println(theNum);
System.out.println(theNum);
System.out.println(theNum);
System.out.println(theNum);
System.out.println(theNum);
System.out.println(theNum);

これにより、同じ数が 10 回出力されます。theNumすべての前に割り当てるかprintln()、数値を配列に格納してループで出力する必要があります。

于 2013-03-02T16:19:56.280 に答える
1

次のコードは明らかに同じ数字を 10 回表示します。

System.out.println (theNum);
System.out.println (theNum);
System.out.println (theNum);
System.out.println (theNum);
System.out.println (theNum);
System.out.println (theNum);
System.out.println (theNum);
System.out.println (theNum);
System.out.println (theNum);
System.out.println (theNum);

やってみてください:

System.out.println ((int) (Math.random () * 10 - 0) + 0);
System.out.println ((int) (Math.random () * 10 - 0) + 0);
System.out.println ((int) (Math.random () * 10 - 0) + 0);
System.out.println ((int) (Math.random () * 10 - 0) + 0);
System.out.println ((int) (Math.random () * 10 - 0) + 0);
System.out.println ((int) (Math.random () * 10 - 0) + 0);
System.out.println ((int) (Math.random () * 10 - 0) + 0);
System.out.println ((int) (Math.random () * 10 - 0) + 0);
System.out.println ((int) (Math.random () * 10 - 0) + 0);
System.out.println ((int) (Math.random () * 10 - 0) + 0);
于 2013-03-02T16:22:31.660 に答える