【问题标题】:ArrayList Calculate Average by dropping SmallestArrayList 通过删除 Smallest 来计算平均值
【发布时间】:2016-07-18 02:57:01
【问题描述】:

我的代码有两个问题。 1)当我向我的数组列表添加值时,它没有给我正确的平均值,所以我知道我的 for 循环中的条件是关闭的,并且 2)它没有显示我的数组列表中的所有数字。它特别不显示第 0 个整数。

这是我的代码:

public class CalcAvgDropSmallest {  

    public static void main(String[] args) {       
       int lowNum = 0;  // # of the lowest numbers to be dropped
       double average;  // calcuates the mean of the sum and the lowest numbers dropped
       ArrayList<Double> inputs= getALInfo();
       lowNum = getLowestnum();
       average = calculateAvg(inputs, lowNum);
       getAvg(inputs, lowNum, average);

    }

    public static ArrayList<Double> getALInfo() {
        ArrayList<Double> inputs = new ArrayList<Double>(); 

        // Read Inputs
          Scanner in = new Scanner(System.in);
          System.out.println("Please enter 5 - 10 integers, Q to quit: ");         
          Double vals = in.nextDouble();

         while (in.hasNextDouble())
            {
               inputs.add(in.nextDouble());
            }             

        return inputs;     
    }   

    public static int getLowestnum() {        
        int lowNum = 0;

        // Reads Input value for # of lowest values dropped

        System.out.println("How many of the lowest values should be dropped?");
        Scanner in = new Scanner(System.in);
        lowNum = in.nextInt();

        return lowNum;

    }

    public static double calculateAvg(ArrayList<Double> inputs, int lowNum) {
        double sum = 0;
        double average = 0;                
        int i = 0;
        // Calcuates the average of the array list with the lowest numbers dropped

        for (i = 0; i < inputs.size(); i++) 
        {
            if (inputs.get(i) > lowNum) {
                sum = sum + inputs.get(i);
            }
        }
        average = (sum / inputs.size());        

       return average;
    }

    public static void getAvg(ArrayList<Double> inputs,int n, double average) {
       // It's adding all the values and dividing by the size of it, which is a problem
       // Also, it's not showing the 0th integer aand just straight to the 1st integer
      System.out.println("The average of the numbers " + inputs + " except the lowest " +n+ " is " +average);  
    }
}

【问题讨论】:

  • 你正在扔掉第一个双输入!
  • 这里:Double vals = in.nextDouble();。不要那样做,不要忽略第一个条目。你输入了 vals,但从来没有把它放到你的 ArrayList 中。

标签: java arraylist average


【解决方案1】:

首先,请编程到List 接口(而不是ArrayList 具体类型)。其次,您应该确保将您读取的每个值添加到您的List。然后,确保您读取了终止值(并丢弃它);否则它将在缓冲区中挂起。最后,我会将Scanner 传递给方法(而不是在本地重新声明它)。喜欢,

public static List<Double> getALInfo(Scanner in) {
    List<Double> inputs = new ArrayList<>();
    System.out.println("Please enter 5 - 10 integers, Q to quit: ");
    while (in.hasNextDouble()) {
        inputs.add(in.nextDouble());
    }
    in.next();
    return inputs;
}

您可以简化 getLowestnum 之类的

public static int getLowestnum(Scanner in) {
    System.out.println("How many of the lowest values should be dropped?");
    return in.nextInt();
}

然后你的calculateAvg 应该对List 进行排序并取一个subList(丢弃lowNum 值),最后返回平均值。比如,

public static double calculateAvg(List<Double> inputs, int lowNum) {
    Collections.sort(inputs);
    return inputs.subList(lowNum, inputs.size()).stream()
            .mapToDouble(Double::doubleValue).average().getAsDouble();
}

然后main完成示例

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    List<Double> inputs = getALInfo(in);
    int lowNum = getLowestnum(in);
    double average = calculateAvg(inputs, lowNum);
    System.out.printf("%.2f%n", average);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2017-09-03
    • 1970-01-01
    • 2023-02-05
    • 2021-02-27
    相关资源
    最近更新 更多