以下是我目前所拥有的:
不过我不知道如何将 0 排除为最小数字。分配要求 0 作为导出编号,因此我需要在最小字符串中出现 0 以外的最小数字。有什么想法吗?
int min, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Value: ");
int val = s.nextInt();
min = max = val;
while (val != 0) {
System.out.print("Enter a Value: ");
val = s.nextInt();
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
};
System.out.println("Min: " + min);
System.out.println("Max: " + max);
最佳答案
这里有一个可能的解决方案:
public class NumInput {
public static void main(String [] args) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Scanner s = new Scanner(System.in);
while (true) {
System.out.print("Enter a Value: ");
int val = s.nextInt();
if (val == 0) {
break;
}
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
}
System.out.println("min: " + min);
System.out.println("max: " + max);
}
}
(不确定是使用 int 还是 double thought)
关于java - 如何从输入的一组数字中获取最大值和最小值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19671453/