尝试在没有数组的情况下打印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);
}
答案 0 :(得分:1)
您应该将最小值初始化为Integer.MAX_VALUE
,将最大值初始化为Integer.MIN_VALUE
,然后在循环中只需删除smallest = randomNum;
即可使用
答案 1 :(得分:-1)
我的问题是哪里是主要的,它没有 public static void main(String [] args)