weather.java

来自「Java经典例程 从外国一大学计算机教授出版物下载的代码 经典」· Java 代码 · 共 107 行

JAVA
107
字号
import java.io.*;
import javagently.*;
import myutilities.*;

class Weather {

  /*  The Weather program    by J M Bishop    Jan 1997
   *  ===================    Graphics July 1999
   *                         updated June 2000
   *
   *  Calculates mean and standard deviation of rainfall
   *  for each month over the number of years provided.
   *
   *  Illustrates handling of a matrices and passing columns
   *  as parameters.
   *  The data must be in a file in the form:
   *  year followed by the 12 rainfall figures for
   *  the months of that year.
   */

  static class RainBase {

    int    base = 1950;
    int    startyear, endyear = 0; // range from 1950 upwards

    // all arrays declared length 13 so months go from 1 to 12
    double rainTable [] [] = new double [13] [70];
    double averagetable [] = new double [13];
    double stddevTable  [] = new double [13];

    Graph g = new Graph("Rainfall", "month", "cm");

    void readIn () throws IOException {
      Stream fin = new Stream("Rain.dat",Stream.READ);
      int actualYear = 0; // e.g. 1989
      int yearIndex = 0;  // e.g. 0

      // The actual years are read in and might not be sorted
      // or contiguous. The yearIndex starts at 0 and is
      // used to store the data in an orderly manner.

      try {
        while (true) {
          actualYear = fin.readInt();
          System.out.print(actualYear+" ");
          if (yearIndex == 0) {
            startyear = actualYear;
          }
          for (int m = 1; m<=12; m++) {
            rainTable[m][yearIndex] = fin.readDouble();
            System.out.print(
                Stream.format(rainTable[m][yearIndex],6,1));
          }
          System.out.println();
          yearIndex++;
        }
      }
      catch (EOFException e) {
        // Pick up the last year of data read in.
        endyear = actualYear;
        System.out.println("Data read for "+startyear+" to "+
            endyear+"\n\n");
      }
    }

    void showResults () {
      System.out.println("Rainfall statistics for " +
          startyear + " to " + endyear);
      System.out.println("========================" +
          "============\n");
      System.out.println("Month\tMean\tStd Deviation");
      int nyears = endyear-startyear+1;
      double a;
      g.setTitle("Mean");
      g.setSymbol(true);

      for (int m =1; m<=12; m++) {
        averagetable[m] = Stats.mean (rainTable[m], nyears);
        stddevTable[m] = Stats.stddev
           (rainTable[m], nyears, averagetable[m]);
        System.out.println(Stream.format(m,2)+
           Stream.format(averagetable[m],12,2)+
           Stream.format(stddevTable[m],12,4));
        g.add(m,averagetable[m]);
      }

      g.nextGraph();
      g.setColor(g.blue);
      g.setSymbol(true);
      g.setTitle("Standard Deviation");
      for (int m = 1; m <= 12; m++) {
        g.add (m, stddevTable[m]);
      }
      g.showGraph();
    }
  }

  public static void main (String args [])throws IOException {

    RainBase rain = new RainBase();

    rain.readIn ();
    rain.showResults ();
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?