Java:如何将随机生成的数字列表中的值放入对象中

Java: How to put values into a object from a list of randomly generated numbers

好的,我是 Java 的新手,这就是我需要做的。 编写一个 Java 程序,允许用户指定要在 1 到 100 之间生成多少个随机数 he/she。然后生成随机数并将它们列在输出中。您还应该计算最高、最低、总和和平均值。我需要帮助的部分是获得最低和最高。我不能使用数组,因为我们还没有真正接触过它们。 我需要帮助从我的 randomInt 中最低的 int 中扔东西。

public static void main(String[] args) 
{
     Scanner sc = new Scanner(System.in);
     Random rnd = new Random();
     int r;
     int sum = 0;
     int lowest=0;
     int highest=0;

     System.out.println("Please enter enter how many numbers "
             + "you would like to generate.");

     int userInput = sc.nextInt();

     for(r=1;r<=userInput;r++) 
    {  // was unsure how to remove the comma from the last number
        int randomInt = (int)(rnd.nextFloat()*100)+1;            
        System.out.print(randomInt + ",");

        sum += randomInt;

        if(randomInt < lowest)
        {
            lowest = randomInt;
        }            
    }    
     System.out.println(" The total sum of these random numbers are " 
             +sum);
     int average = sum / userInput;
     System.out.println("The average of all those numbers is "
             + average);
     System.out.println(lowest);
}

您的 lowest 被分配为 0,您随机生成数字的范围是 1 到 100。在您的代码中,lowest 的值唯一发生变化的时间是条件 randomInt < lowest就满足了,当然你的程序总是输出0作为最低。

尝试将 lowest 的值更改为 100。