java用一个带有另一个数组值的数组循环一个方法

时间:2016-11-30 02:57:10

标签: java arrays methods

我有两个数组:

  • 2015年每天都有高温的人。
  • 每月有一天的天数(例如第一个索引是31然后是28,依此类推)。

我需要制作一个找到一个月的平均高温的方法,然后在main中,我需要编写一个循环来循环使用该方法,每个月平均并打印它们。

这是我对方法的尝试

public static double monthaverage(int[] a, int [] b, int c) {
    int j=b[c];
    int total = 0;

    for (int i = 0; i <j; i++)
            total += a[i];
            double average = (double)total/ (double) j;

    return average; 
} 

而且,我在主

中尝试循环和数据
    int[] tmax = {32, 38, 34, 35, 41, 17, 25, 17, 29, 24, 26, 33, 31, 24, 
                  29, 38, 20, 49, 49, 36, 31, 38, 35, 32, 37, 20, 17, 26, 
                  30, 32, 22, 26, 12, 20, 35, 34, 19, 28, 22, 15, 30, 23, 
                  20, 17, 16, 19, 21, 21, 32, 33, 19, 34, 35, 31, 19, 34, 
                  21, 27, 27, 30, 36, 32, 46, 39, 23, 38, 40, 44, 47, 56, 
                  41, 39, 38, 36, 45, 44, 28, 32, 34, 36, 35, 34, 39, 42, 
                  49, 49, 41, 41, 40, 48, 45, 46, 66, 49, 48, 41, 47, 42, 
                  35, 43, 54, 68, 66, 70, 65, 55, 67, 55, 57, 48, 63, 60, 
                  53, 54, 55, 56, 58, 63, 57, 60, 55, 54, 62, 76, 75, 72, 
                  84, 58, 59, 83, 68, 82, 64, 68, 70, 63, 74, 61, 65, 67, 
                  69, 67, 65, 83, 84, 91, 79, 80, 77, 84, 73, 51, 50, 61, 
                  60, 58, 73, 67, 65, 68, 81, 86, 80, 85, 78, 61, 61, 75, 
                  72, 80, 69, 72, 72, 67, 82, 78, 67, 70, 59, 69, 75, 68, 
                  78, 80, 71, 82, 82, 76, 84, 72, 84, 87, 90, 78, 76, 82, 
                  76, 74, 70, 81, 84, 70, 82, 78, 76, 67, 67, 77, 83, 88, 
                  86, 86, 86, 81, 81, 80, 82, 80, 76, 80, 77, 77, 67, 80, 
                  77, 80, 85, 85, 89, 86, 83, 75, 73, 78, 70, 79, 75, 80, 
                  79, 77, 75, 81, 86, 80, 84, 86, 72, 78, 82, 92, 89, 86, 
                  78, 73, 74, 62, 73, 83, 85, 82, 83, 75, 72, 69, 65, 74, 
                  74, 63, 63, 67, 74, 75, 69, 62, 55, 58, 58, 61, 69, 67, 
                  63, 59, 56, 68, 70, 62, 68, 57, 61, 57, 46, 48, 66, 58, 
                  65, 54, 47, 62, 54, 52, 59, 73, 58, 51, 58, 64, 64, 64, 
                  68, 69, 65, 53, 58, 53, 47, 53, 60, 46, 53, 54, 47, 47, 
                  53, 59, 46, 42, 42, 42, 41, 51, 61, 57, 41, 32, 38, 44, 
                  45, 47, 51, 51, 57, 39, 45, 53, 48, 57, 47, 48, 56, 42, 
                  50, 46, 40, 38, 47, 49, 47, 51, 62, 51, 43, 34, 23, 28, 
                  44};
      int [] monthlength= {31,28,31,30,31,30,31,31,30,31,30,31};
      for (int i = 0; i <12; i++){
        average= monthaverage(tmax, monthlength, c);
        System.out.println("Average: test " + average);
        c++;
    }

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码来获取平均温度。请注意我已经使用main方法添加了SampleTemperature类。

public class SampleTemperature {
public static double monthaverage(int[] a, int[] b, int c) {
    int j = b[c];
    int total = 0;
    int totalDays=0;
    for (int numOfMonth=0;(numOfMonth<c);numOfMonth++) {
        totalDays+=b[numOfMonth];
    }
    //System.out.println(c+"Start Date:"+totalDays+" Number of days"+j);
    int startdate=totalDays;
    for (int i = startdate; i < startdate+j; i++)
        total += a[i];
    double average = (double) total / (double) j;

    return average;
}

public static void main(String[] args) {
    int[] tmax = { 32, 38, 34, 35, 41, 17, 25, 17, 29, 24, 26, 33, 31, 24, 29, 38, 20, 49, 49, 36, 31, 38, 35, 32,
            37, 20, 17, 26, 30, 32, 22, 26, 12, 20, 35, 34, 19, 28, 22, 15, 30, 23, 20, 17, 16, 19, 21, 21, 32, 33,
            19, 34, 35, 31, 19, 34, 21, 27, 27, 30, 36, 32, 46, 39, 23, 38, 40, 44, 47, 56, 41, 39, 38, 36, 45, 44,
            28, 32, 34, 36, 35, 34, 39, 42, 49, 49, 41, 41, 40, 48, 45, 46, 66, 49, 48, 41, 47, 42, 35, 43, 54, 68,
            66, 70, 65, 55, 67, 55, 57, 48, 63, 60, 53, 54, 55, 56, 58, 63, 57, 60, 55, 54, 62, 76, 75, 72, 84, 58,
            59, 83, 68, 82, 64, 68, 70, 63, 74, 61, 65, 67, 69, 67, 65, 83, 84, 91, 79, 80, 77, 84, 73, 51, 50, 61,
            60, 58, 73, 67, 65, 68, 81, 86, 80, 85, 78, 61, 61, 75, 72, 80, 69, 72, 72, 67, 82, 78, 67, 70, 59, 69,
            75, 68, 78, 80, 71, 82, 82, 76, 84, 72, 84, 87, 90, 78, 76, 82, 76, 74, 70, 81, 84, 70, 82, 78, 76, 67,
            67, 77, 83, 88, 86, 86, 86, 81, 81, 80, 82, 80, 76, 80, 77, 77, 67, 80, 77, 80, 85, 85, 89, 86, 83, 75,
            73, 78, 70, 79, 75, 80, 79, 77, 75, 81, 86, 80, 84, 86, 72, 78, 82, 92, 89, 86, 78, 73, 74, 62, 73, 83,
            85, 82, 83, 75, 72, 69, 65, 74, 74, 63, 63, 67, 74, 75, 69, 62, 55, 58, 58, 61, 69, 67, 63, 59, 56, 68,
            70, 62, 68, 57, 61, 57, 46, 48, 66, 58, 65, 54, 47, 62, 54, 52, 59, 73, 58, 51, 58, 64, 64, 64, 68, 69,
            65, 53, 58, 53, 47, 53, 60, 46, 53, 54, 47, 47, 53, 59, 46, 42, 42, 42, 41, 51, 61, 57, 41, 32, 38, 44,
            45, 47, 51, 51, 57, 39, 45, 53, 48, 57, 47, 48, 56, 42, 50, 46, 40, 38, 47, 49, 47, 51, 62, 51, 43, 34,
            23, 28, 44 };
    int[] monthlength = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    for (int i = 0; i < monthlength.length; i++) {
        double average = monthaverage(tmax, monthlength, i);
        System.out.println("Average: test " + average);
    }
}
}
根据评论

更新了

答案 1 :(得分:0)

这是我的版本,它基本上和@Coder一样,带有一些额外的打印输出/几个额外的命名变量,可以更轻松地跟随它们。

首先,您会注意到main()方法基本相同。它在一年中的每个月循环(分别通过索引0-11)并打印出平均温度。循环中的另一个println调用只是一个可视化来分隔输出文本。

public static void main(String[] args) throws Exception {
    for (int i = 0; i < 12; ++i) {
        System.out.println(new String(new char[80]).replace("\0", "-"));
        System.out.println("Average: test " + monthAverage(i));
    }
}

其次,你会注意到monthAverage()方法乍一看是相当大的,但事实上它可以减少很多 - 大部分膨胀都来自变量的正确命名,以更好地帮助说明每个各自用于。另请注意:camelCase是遵循java的好习惯。

public static double monthAverage(int monthIndex) {
    if (monthIndex < 0 || monthIndex > 11)
        throw new IndexOutOfBoundsException("month index out of range.");
    double result = 0L;
    int startOffset = 0;
    for (int i = 0; i < monthIndex; ++i)
        startOffset += monthLength[i];
    int len = monthLength[monthIndex];
    int endOffset = startOffset + len;
    System.out.printf("Day #'s: start=%03d, end=%03d, elapsed=%d, local month=%s",
            startOffset, endOffset, len, new DateFormatSymbols().getMonths()[monthIndex]);
    System.out.println();
    for (int i = startOffset; i < endOffset; ++i) {
        double dayTemp = (double) tmax[i];
        result += dayTemp;
    }
    return (result / len);
}

monthAverage()从确保索引是正确的索引开始。然后计算开始/结束日期。最后取出并返回月份的平均值。

总之它看起来像这样:

import java.text.DateFormatSymbols;

public class App {
    public static final int[] tmax = { 32, 38, 34, 35, 41, 17, 25, 17, 29, 24, 26, 33, 31, 24, 29, 38, 20, 49, 49, 36,
            31, 38, 35, 32, 37, 20, 17, 26, 30, 32, 22, 26, 12, 20, 35, 34, 19, 28, 22, 15, 30, 23, 20, 17, 16, 19, 21,
            21, 32, 33, 19, 34, 35, 31, 19, 34, 21, 27, 27, 30, 36, 32, 46, 39, 23, 38, 40, 44, 47, 56, 41, 39, 38, 36,
            45, 44, 28, 32, 34, 36, 35, 34, 39, 42, 49, 49, 41, 41, 40, 48, 45, 46, 66, 49, 48, 41, 47, 42, 35, 43, 54,
            68, 66, 70, 65, 55, 67, 55, 57, 48, 63, 60, 53, 54, 55, 56, 58, 63, 57, 60, 55, 54, 62, 76, 75, 72, 84, 58,
            59, 83, 68, 82, 64, 68, 70, 63, 74, 61, 65, 67, 69, 67, 65, 83, 84, 91, 79, 80, 77, 84, 73, 51, 50, 61, 60,
            58, 73, 67, 65, 68, 81, 86, 80, 85, 78, 61, 61, 75, 72, 80, 69, 72, 72, 67, 82, 78, 67, 70, 59, 69, 75, 68,
            78, 80, 71, 82, 82, 76, 84, 72, 84, 87, 90, 78, 76, 82, 76, 74, 70, 81, 84, 70, 82, 78, 76, 67, 67, 77, 83,
            88, 86, 86, 86, 81, 81, 80, 82, 80, 76, 80, 77, 77, 67, 80, 77, 80, 85, 85, 89, 86, 83, 75, 73, 78, 70, 79,
            75, 80, 79, 77, 75, 81, 86, 80, 84, 86, 72, 78, 82, 92, 89, 86, 78, 73, 74, 62, 73, 83, 85, 82, 83, 75, 72,
            69, 65, 74, 74, 63, 63, 67, 74, 75, 69, 62, 55, 58, 58, 61, 69, 67, 63, 59, 56, 68, 70, 62, 68, 57, 61, 57,
            46, 48, 66, 58, 65, 54, 47, 62, 54, 52, 59, 73, 58, 51, 58, 64, 64, 64, 68, 69, 65, 53, 58, 53, 47, 53, 60,
            46, 53, 54, 47, 47, 53, 59, 46, 42, 42, 42, 41, 51, 61, 57, 41, 32, 38, 44, 45, 47, 51, 51, 57, 39, 45, 53,
            48, 57, 47, 48, 56, 42, 50, 46, 40, 38, 47, 49, 47, 51, 62, 51, 43, 34, 23, 28, 44 };
    public static final int[] monthLength = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 12; ++i) {
            System.out.println(new String(new char[80]).replace("\0", "-"));
            System.out.println("Average: test " + monthAverage(i));
        }
    }

    /**
     * monthAverage method
     * 
     * @param monthIndex
     *            The given month average to compute (e.g. 0 = jan. 11 = dec.)
     * @return the average temp
     */
    public static double monthAverage(int monthIndex) {
        if (monthIndex < 0 || monthIndex > 11)
            throw new IndexOutOfBoundsException("month index out of range.");
        double result = 0L;
        int startOffset = 0;
        for (int i = 0; i < monthIndex; ++i)
            startOffset += monthLength[i];
        int len = monthLength[monthIndex];
        int endOffset = startOffset + len;
        System.out.printf("Day #'s: start=%03d, end=%03d, elapsed=%d, local month=%s",
                startOffset, endOffset, len, new DateFormatSymbols().getMonths()[monthIndex]);
        System.out.println();
        for (int i = startOffset; i < endOffset; ++i) {
            double dayTemp = (double) tmax[i];
            result += dayTemp;
        }
        return (result / len);
    }
}

这是我的输出:

--------------------------------------------------------------------------------
Day #'s: start=000, end=031, elapsed=31, local month=January
30.548387096774192
--------------------------------------------------------------------------------
Day #'s: start=031, end=059, elapsed=28, local month=February
24.642857142857142
--------------------------------------------------------------------------------
Day #'s: start=059, end=090, elapsed=31, local month=March
39.41935483870968
--------------------------------------------------------------------------------
Day #'s: start=090, end=120, elapsed=30, local month=April
54.86666666666667
--------------------------------------------------------------------------------
Day #'s: start=120, end=151, elapsed=31, local month=May
71.35483870967742
--------------------------------------------------------------------------------
Day #'s: start=151, end=181, elapsed=30, local month=June
69.73333333333333
--------------------------------------------------------------------------------
Day #'s: start=181, end=212, elapsed=31, local month=July
78.54838709677419
--------------------------------------------------------------------------------
Day #'s: start=212, end=243, elapsed=31, local month=August
79.35483870967742
--------------------------------------------------------------------------------
Day #'s: start=243, end=273, elapsed=30, local month=September
76.06666666666666
--------------------------------------------------------------------------------
Day #'s: start=273, end=304, elapsed=31, local month=October
59.483870967741936
--------------------------------------------------------------------------------
Day #'s: start=304, end=334, elapsed=30, local month=November
53.0
--------------------------------------------------------------------------------
Day #'s: start=334, end=365, elapsed=31, local month=December
45.83870967741935

希望有所帮助,让我知道是否有任何我遗漏/需要解释/不正确,欢呼。