java - 在Java中,我如何构造一个for循环来找到输入的十个值的最大值和最小值。我目前没有收到该程序的输出

标签 java for-loop

我想做的是从用户那里接收十个值,精确到小数点后第十位。然后我想找到最大值和最小值,并仅显示它们。我尝试了许多不同的配置,这个配置最具逻辑性,但仍然无法获得任何类型的输出。

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/

相关文章:

java - 在多维数组中 move 对象

java - 错误 HTTP : 500 when uploading/sharing file in openmeetings

java - java中的电话号码验证

javascript - 在javascript中找到最大整数后返回数组的一部分?

R:使用 "doparallel"、 "foreach"和 "purrr"库重写循环

powershell - 在Powershell中执行For循环以执行x次不同的服务器重启

JavaScript for 循环帮助

java - 如何mvn部署-部署:file from java app

excel - 将 For Each 中的 Variant 转换为范围

java - 使用 ADB 截取屏幕截图并在 java 中检索它而无需写入文件