我被分配编写一个程序,读取一系列整数输入和打印 - 最小和最大的输入 - 以及偶数和奇数输入的数量
我想出了第一部分,但我很难知道如何让我的程序显示最大和最小.到目前为止这是我的代码.如何让它显示最小的输入?
public static void main(String args[])
{
Scanner a = new Scanner (System.in);
System.out.println("Enter inputs (This program calculates the largest input):");
double largest = a.nextDouble();
while (a.hasNextDouble())
{
double input = a.nextDouble();
if (input > largest)
{
largest = input;
}
}
System.out.println(largest);
}
Run Code Online (Sandbox Code Playgroud)
最简单的解决办法是使用像Math.min和Math.max
double largest = a.nextDouble();
double smallest = largest;
while (a.hasNextDouble()) {
double input = a.nextDouble();
largest = Math.max(largest, input);
smallest = Math.min(smallest, input);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15247 次 |
| 最近记录: |