我正在尝试使用循环创建Java代码,该循环将接受来自控制台的五个整数输入并返回最小和最大。我遇到了代码聚集的问题。谢谢你
import java.util.*;
public class Highest_LowestofFiveIntegers {
public static void main(String[] args) {
System.out.print("Enter five integers separated by a
space i.e. 2 6 55 1 3 910: ");
int number = 0;
number = input.nextInt();
int max = 0;
int min = 0;
for (int x = 0; x<5; x++){
if (x == 0 || number > max){
max = number;
}
if (x == 0 || number < min){
min = number;
}
}
System.out.println("Highest value: " + max);
System.out.println("Lowest value: " + min);
}
}
答案 0 :(得分:2)
将number = input.nextInt();
放入 for
循环中;它之前不。
int max = 0, min = 0;
for (int x = 0; x < 5; x++) {
int number = input.nextInt();
if (x == 0 || number > max){
max = number;
}
if (x == 0 || number < min){
min = number;
}
}
System.out.println("Highest value: " + max);
System.out.println("Lowest value: " + min);