【问题标题】:finding min, max, and average with different character type查找具有不同字符类型的最小值、最大值和平均值
【发布时间】:2014-08-05 05:44:06
【问题描述】:

我成功地在我的代码中找到了最小值、最大值和平均值。像这样的代码:

import java.util.Scanner;

public class Tugas {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int max = Integer.MIN_VALUE; 
    int min = Integer.MAX_VALUE; 

    int    count       = 0;     
    double countTotal = 0;

    //... Buat Perulangannya
    System.out.print("Enter values (separate by space) : ");

    while (input.hasNextDouble() ) { 
        //... Get the next value.
        double value = input.nextDouble();

        //... Compare this value to max and min. Replace if needed.
        if (value > max) {
            max = (int) value;
        }
        if (value < min) {
            min = (int) value;
        }

        //... Keep track of these values for average calculation.
        count++;                 // Count the number of data points.
        countTotal += value;   // Keep a running total.
    }

    //... Be sure user entered at least one data point.
    if (count > 0) {                                          //Note 2
        //... Display statistics
        double rata = countTotal / count;

        System.out.println("Min = "  + min);
        System.out.println("Max = "  + max);
        System.out.println("Average = "  + rata);
    } else {
        System.out.println("No Data...!");
    }
}

}

ok,如果我正在输入: 输入值:1 2 3 4 5, 输出为:min 1,max= 5,average = 3.0

问题是,当我按下“输入”时,我的代码不会自动计数。

当我输入时如何输入:a b 8 9 7 c d 它会一直循环直到得到正确的输入

所以: 输入值:a b 8 9 7 c d 它打印“请输入数字类型” 输入值:6 2 4 6 7 2 3 5 3 分钟:2 最大:7 平均:4.2222

谢谢

【问题讨论】:

    标签: java max min


    【解决方案1】:

    这里有一个小的修改,它接受来自扫描器的整行,然后解析它。它会根据您的规范在解析时验证该行,如果输入无效则循环。

    您确定要将 min 和 max 强制转换为 int 类型吗?该应用程序的其余部分似乎处理双打。

    导入 java.util.Scanner;

    public class Tugas {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int max = Integer.MIN_VALUE;
            int min = Integer.MAX_VALUE;
    
            int count = 0;
            double countTotal = 0;
    
            //... Buat Perulangannya
            boolean validinput = false;
            String valueSetLine = null;
            while (!validinput) {
                System.out.print("Enter values (separate by space) : ");
                valueSetLine = input.nextLine();
                if (valueSetLine.matches("^(-|-?\\d*\\.?\\d+(\\s-?\\d*\\.?\\d*)*)$"))  {
                    System.out.println("valid string");
                    validinput = true;
                } else {
                    System.out.println("invalid input");
                }
            }
            String[] doubleStrs = valueSetLine.split("\\s");
            double[] values = new double[doubleStrs.length];
            int i = 0;
            for (String s : doubleStrs) {
                try {
                    double d = Double.parseDouble(s);
                    values[i] = d;
                    i++;
                } catch (NumberFormatException nfe) {
                    nfe.printStackTrace();
                }
            }
    
            for (double value : values) {
                //... Compare this value to max and min. Replace if needed.
                if (value > max) {
                    max = (int) value;
                }
                if (value < min) {
                    min = (int) value;
                }
    
                //... Keep track of these values for average calculation.
                count++;                 // Count the number of data points.
                countTotal += value;   // Keep a running total.
            }
    
            //... Be sure user entered at least one data point.
            if (count > 0) {                                          //Note 2
                //... Display statistics
                double rata = countTotal / count;
    
                System.out.println("Min = " + min);
                System.out.println("Max = " + max);
                System.out.println("Average = " + rata);
            } else {
                System.out.println("No Data...!");
            }
    
        }
    
    }
    

    【讨论】:

    • 非常感谢您。你解决这个问题的方法太棒了。但是,当我输入时如何获得正确的输出:a b 8 9 7 c d。输出将循环直到获得正确的输入,所以输出如下:输入值:ab 8 9 7 cd,它打印“请输入数字类型”输入值:6 2 4 6 7 2 3 5 3 min:2最大值:7 平均值:4.2222 和“恭喜”。谢谢...梅尔
    • 如果我对您的理解正确,只需更改行 `System.out.println("valid string");有效输入=真; } else { System.out.println("无效输入"); } ` 到 ` 有效输入 = true; } else { System.out.println("请输入数字类型。"); } ` 它已经设置为循环,直到输入正确。唯一的区别在于打印输出。
    • 好的,谢谢。所以在我的java新手中帮助我。再次感谢您
    猜你喜欢
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2014-06-11
    • 1970-01-01
    • 2016-01-05
    • 2021-01-15
    相关资源
    最近更新 更多