-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);

这会打印十次相同的数字。您需要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 回答