【问题标题】:Finding the min max and average of an array查找数组的最小最大值和平均值
【发布时间】:2014-06-11 18:20:55
【问题描述】:

我需要找到余额的最小值、最大值和平均值。我在使用 for 循环之前已经这样做了,但从来没有使用过 while 循环。有没有办法在不干扰 while 循环的情况下直接从数组中提取最小值最大值和平均值?

10
Helene 1000
Jordan 755
Eve 2500
Ken 80
Andrew 999
David 1743
Amy 12
Sean 98
Patrick 7
Joy 14

其中 10 是帐户数

import java.util.*;
import java.io.*;
import java.util.Arrays;

public class bankaccountmain {

public static void main(String[] args) throws FileNotFoundException {
    Scanner inFile = null;
    try {
        inFile = new Scanner(new File("account.txt"));
        ;
    } catch (FileNotFoundException e) {
        System.out.println("File not found!");

        System.exit(0);
    }
    int count = 0;
    int accounts = inFile.nextInt();
    String[] names = new String[accounts];
    int[] balance = new int[accounts];
    while (inFile.hasNextInt()) {
        inFile.next();
        names[count] = inFile.next();
        inFile.nextInt();
        balance[count] = inFile.nextInt();
        count++;
    }
    System.out.println(Arrays.toString(names));
    System.out.println(Arrays.toString(balance));
    }
}

【问题讨论】:

  • 那你遇到了什么问题?
  • 呃...使用 while 循环和 for 循环执行此操作的方式没有区别;您在循环外为 min、max、total 和 count 设置变量。你循环你的 inFile 东西来更新最小值、最大值、总数和计数。当你退出循环时,你有最小值,最大值,平均值是总数/计数。你有什么问题?
  • 在 Java 8 中,使用集合和流 API,您可以在没有循环的情况下执行此操作,但我不确定您的确切问题是什么,我也不太了解您的代码做了,或者它应该做什么。

标签: java arrays max average min


【解决方案1】:

当您将值存储在balance[count] 中时,您已经从文件中提取了这些值。然后,您可以使用刚刚读取的这些值进行计算:

    int min = Integer.MAX_VALUE;
    int max = 0;
    int total = 0;
    while (inFile.hasNext()) {
        names[count] = inFile.next();
        balance[count] = inFile.nextInt();  // balance[count] now is storing the value from the file

        if (balance[count] < min) {  // so I can use it like any other int
            min = balance[count];
        }
        if (balance[count] > max) {  // like this
            max = balance[count];
        }
        total += balance[count]; // and this
        count++;
    }
    double avg = (double)total/count;

【讨论】:

    【解决方案2】:

    azurefrog 的答案是正确且可以接受的 (+1)。

    但我个人更喜欢避免“混合”过多的功能。在这种情况下:我认为

    • 从文件中读取值并
    • 计算最小值/最大值/平均值

    应该是单独的操作。

    计算int[] 数组的最小值/最大值/平均值是一种非常常见的操作。如此普遍,以至于它可能已经被实现了数千次(遗憾的是,Java 8 之前的标准 API 中没有此功能)。

    但是,如果我必须实现这样的东西(并且不愿意或不允许使用复杂的库解决方案,例如 Apache Commons Math 的 SummaryStatistics),我会选择一些实用方法:

    static int min(int array[]) 
    {
        int result = Integer.MAX_VALUE;
        for (int a : array) result = Math.min(result, a);
        return result;
    }
    
    int max(int array[]) 
    {
        int result = Integer.MIN_VALUE;
        for (int a : array) result = Math.max(result, a);
        return result;
    }
    
    static int sum(int array[]) 
    {
        int result = 0;
        for (int a : array) result += a;
        return result;
    }
    
    static double average(int array[]) 
    {
        return (double)sum(array)/array.length;
    }
    

    另一个优点(除了“关注点分离”的“纯粹主义者”论点之外)是这些方法是可重用的。您可以将它们放入IntArrays 实用程序类中,当您需要此功能时,只需调用这些方法即可。

    如果您已经可以使用 Java 8,那就更简单了,因为这些操作现在(终于!)标准 API 的一部分。它们是在 IntStream 类中提供的,它恰好具有您需要的 min()max()average() 方法。在那里你可以写一些类似的东西

    int min = IntStream.of(balance).min().getAsInt();
    int max = IntStream.of(balance).max().getAsInt();
    double average = IntStream.of(balance).average().getAsDouble();
    

    【讨论】:

    • 这种情况下需要读取数组3次,而azurefrog只读取一次
    • @Lordofdark 我提到数组只能读取一次。
    猜你喜欢
    • 1970-01-01
    • 2021-01-15
    • 2021-06-18
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2017-07-24
    • 2020-06-17
    相关资源
    最近更新 更多