如何在没有数组的情况下找到最大和最小的50个随机数?

时间:2016-11-23 14:45:53

标签: java

尝试在没有数组的情况下打印Java中最大和最小的50个随机数。它不起作用,我很困惑。有人可以帮忙吗?谢谢!

public static void main() {
    int smallest = 0, biggest = 0;
    for (int i = 0; i < 50; i++) {
        int randomNum = (int)(Math.random() * 100) + 1;
        System.out.println(randomNum);
        smallest = randomNum;
        if (randomNum > biggest) {
            biggest = randomNum;
        }
        if (randomNum < smallest) {
            smallest = randomNum;
        }
    }
    System.out.println("Smallest was " + smallest + ", biggest was " + biggest);
}

2 个答案:

答案 0 :(得分:1)

您应该将最小值初始化为Integer.MAX_VALUE,将最大值初始化为Integer.MIN_VALUE,然后在循环中只需删除smallest = randomNum;即可使用

答案 1 :(得分:-1)

我的问题是哪里是主要的,它没有     public static void main(String [] args)