我的Java方法获取总计在我的main方法中不起作用

时间:2015-03-14 21:49:14

标签: java io

我的月度降雨计划的第2版,其中包含了编辑和建议。非常感谢你的帮助,这真让我思考。关于编程的精彩之处在于陌生人如何能够齐聚一堂来解决问题。

// Rainfall Class Start 

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Arrays;

/**
 *
 * @author Adrian
 */
public class Rainfall{
   DecimalFormat twoDecimals = new DecimalFormat("##0.00");
   private final String[] Months={"Jan","Feb","Mar","Apr","May","Jun",
                           "Jul","Aug","Sep","Oct","Nov","Dec"};;
   private final double[] Rainfall;

   public Rainfall() throws IOException
   {
      Rainfall = new double[12];
   }
   public double getAverageRainFall()
   {
      return getTotalRainFall() / 12 ;
   }
   public void outputToFile()throws IOException
       {
       try (PrintWriter outputFile = new PrintWriter("Months.txt")) {
           Scanner kb=new Scanner(System.in);

           System.out.println("For each month enter a rainfall amount");

           for (int i=0;i<12;i++)
           {
               System.out.println(Months[i]+"?");
               Rainfall[i]=kb.nextDouble();
               outputFile.println(Months[i]+" "+Rainfall[i]);
               System.out.println(Months[i]+" "+Rainfall[i]);
           }
       }
       } 

    /**
     *
     * @return 
     */
    public double getTotalRainFall()
   {
      double total = 0.0;  // Accumulator initialized to 0

      // Accumulate the sum of the rain array elements.
      for (int i = 0; i < Rainfall.length; i++)
         total += Rainfall[i];

      // Return the sum.
      return total;
   }
    public double getLowestMonth()
   {
      int lowest = 0;

      // Find the element with the lowest value.
      for (int i = 1; i < Rainfall.length; i++)
      {
         if (Rainfall[i] < Rainfall[lowest])
            lowest = i;
      }
      // Return the element number.
      return lowest+1;
      }
    public int getHighestMonth()
   {
      int highest = 0;

      // Find the element with the highest value.
      for (int i = 1; i < Rainfall.length; i++)
      {
         if (Rainfall[i] > Rainfall[highest])
            highest = i;
      }
      // Return the element number.
      return highest;
   }
    public void displayMessage(){
          System.out.println("----------------------");
          System.out.println("Monthly Rainfall Totals For 2014"); 
          System.out.println(Arrays.toString(Months));
          System.out.println(Arrays.toString(Rainfall));
          System.out.println("Average Rainfall = "  +getAverageRainFall());
          System.out.println("Total Rainfall   = "  +getTotalRainFall());
          System.out.println("Highest Month    = "  +getHighestMonth());
          System.out.println("Lowest Month     = "  +getLowestMonth());
          System.out.println("----------------------");
}
    public double getRainAt(int e)
   {
      return Rainfall[e];
   }
   }



  /*public void inputFromFile() throws IOException
   {
      File myFile=new File ("Months.txt");
      Scanner inputFile=new Scanner(myFile);

      //loop through the file

      for(int i=0;i<12;i++){
         Months[12] = inputFile.nextLine();
         inputFile.close();
      }
   }*/

//结束课程    //主要方法的开始

import java.io. *;    import java.text.DecimalFormat;

public class Rainfall2{

    public static void main(String[] args) throws IOException
      {
             // The low month

         DecimalFormat twoDecimals = new DecimalFormat("##0.00");
         Rainfall myData=new Rainfall();

         myData.outputToFile();
         myData.displayMessage();

      }
}

我的月度降雨计划的第2版,其中包含了编辑和建议。非常感谢你的帮助,这真让我思考。关于编程的精彩之处在于陌生人如何能够齐聚一堂来解决问题。

3 个答案:

答案 0 :(得分:3)

编译器实际上是正确的,你不能从静态main调用非stati方法:),在getTotalRainFall();的实例上调用Rainfall

Edit1:谢谢puj,修正了错误。

Edit2:实际上,你的代码看起来有些混乱,那个什么是Price类?你应该有降雨吗?最终结束如下:

RainFall rf=new RainFall();
rf.inputFromFile();
rf.getTotalRainFall();
rf.outputToFile();

水晶,我可以建议你从头开始重写它,并从这些答案中得到建议吗?

答案 1 :(得分:2)

此外,您可能想要更改

  for(int i=0;i<12;i++)
     Months[i]=inputFile.nextLine();
     inputFile.close();

  for(int i=0;i<12;i++)
     Rainfall[i]=inputFile.nextLine();
     inputFile.close();

获取正确的数据。

答案 2 :(得分:0)

public double getTotalRainfall()静态,

public static double getTotalRainfall(){}

因此您可以使用main

double totalRain = Rainfall.getTotalRainfall();拨打电话

您必须将private double[] rainFall;设为静态,private static double[] rainFall;

根据Java中的标准命名,你应该使用数组RainfallMonths的另一个名称,你应该使用小写动词来声明字段。否则可能会导致问题。