我想做的是从用户那里接收十个值,精确到小数点后第十位。然后我想找到最大值和最小值,并仅显示它们。我尝试了许多不同的配置,这个配置最具逻辑性,但仍然无法获得任何类型的输出。
import java.util.Scanner;
public class Lab5b
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
for (int counter = 0; counter <= 10; counter++ )
{
double currentMin = in.nextDouble();
while (in.hasNextDouble())
{
double input = in.nextDouble();
if (input < currentMin)
{
currentMin = input;
}
}
double currentMax = in.nextDouble();
while (in.hasNextDouble())
{
double input = in.nextDouble();
if (input > currentMax)
{
currentMax = input;
}
}
System.out.println(currentMax);
System.out.print(currentMin);
}
}
}
最佳答案
这是可能的解决方案之一......
import java.util.Scanner;
public class MinMaxForLoop {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
double current;
System.out.println("Enter 10 double values:");
for (int i = 0; i < 10; i++) {
System.out.print((i+1) + ". -> ");
current = input.nextDouble();
if(current < min)
min = current;
else if(current > max)
max = current;
}
System.out.println("Min: " + min);
System.out.println("Max: " + max);
}
}
关于java - 在Java中,我如何构造一个for循环来找到输入的十个值的最大值和最小值。我目前没有收到该程序的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29188235/