Java从我的循环外部的数据打印我的去年有困难

时间:2016-12-07 06:14:45

标签: java loops

我正在尝试使用我的方法和文件打印我文件上32度以下的天数以及80以上的天数以及最高温度的平均值。我从一个文件中读取了所有数据并打印了我教授之前想要的其他内容,但是当我将其更改为打印下面和上面的日期时,它不会打印出去年的温度数据。

以下是教授希望工作的其他内容的第一个循环的代码:

    int currentYear= 1942;
    int prevOccurrence=0;
    for (i=0;i<=73;i++){
        occurrence = occurrence(year, currentYear);
        System.out.println(currentYear-1 +" [" + prevOccurrence + ", " + occurrence +")");
        prevOccurrence+= occurrence-prevOccurrence;
        currentYear++;
    }
    System.out.println(currentYear-1 +" [" + prevOccurrence + ", " + (year.length)+")");

这是输出:

1941 [0, 365)
1942 [365, 730)
1943 [730, 1095)
1944 [1095, 1461)
1945 [1461, 1826)
1946 [1826, 2191)
...
2009 [24837, 25202)
2010 [25202, 25567)
2011 [25567, 25932)
2012 [25932, 26298)
2013 [26298, 26663)
2014 [26663, 27028)
2015 [27028, 27393)

从1941年到2015年,2015年有数据

在此循环中,尝试查找低于或高于其值的天数,但未提供2015年的任何数据

currentYear= 1942;
    prevOccurrence=0;
    count =0;
    int lt32=0;
    int gravg=0;
    int gre80=0;
    average = average(tmax);
    for (i=0;i<=73;i++){
        occurrence = occurrence(year, currentYear);
        lt32 = (occurrence - prevOccurrence) - count(tmin,  prevOccurrence,  occurrence, 32.0);
        gravg= count(tmax,  prevOccurrence,  occurrence, average);
        gre80= count(tmax,  prevOccurrence,  occurrence, 79.0);
        System.out.println(currentYear-1 +" " + lt32 + " " + gravg +" " + gre80);
        prevOccurrence+= occurrence-prevOccurrence;
        currentYear++;

    }
    prevOccurrence+= year.length-prevOccurrence;
    lt32 = (year.length - prevOccurrence) - count(tmin,  prevOccurrence,  year.length, 32.0);
    gravg= count(tmax,  prevOccurrence,  occurrence, average);
    gre80= count(tmax,  prevOccurrence,  year.length, 79.0);
    System.out.println(currentYear-1 +" " + lt32 + " " + gravg +" " + gre80 );

输出

1941 160 191 51
1942 164 191 35
1943 171 180 38
1944 180 182 52
1945 150 183 38
1946 157 198 36
...
2009 139 190 28
2010 129 202 60
2011 134 193 45
2012 139 196 46
2013 151 192 38
2014 139 189 39
2015 0 0 0

以下是我用它的方法

public static double average(int[] a) {
    int total = 0;
    for (int i = 0; i < a.length; i++)
        total += a[i];
    double average = (double)total/ (double) a.length;
    return average;
}

public static int arrayMax(int[] a) {
    int max = a[0];
    for (int i = 0; i < a.length; i++)
        if (a[i] > max)
            max = a[i];
    return max; 
} 

public static int arrayMin(int[] a) {
    int min = a[0];
    for (int i = 0; i < a.length; i++)
        if (a[i] < min)
            min = a[i];
    return min; 
}  

public static int occurrence(int[] a, int value) {
    int index = 0;
    for (int i = 0; i < a.length; i++)
        if (a[i] == value){
            index = i;
            value = index;
        }
    if (value!=index){
        index=-1;
    }
    return index; 
}

public static int count(int[] a, int startIndex, int endIndex, double value) {
    int count=0;
    for (int i = startIndex; i < endIndex; i++)
        if (a[i] > value)
            count++;
    return count; 
}  

0 个答案:

没有答案