我怎么能修复我的代码?

时间:2015-12-01 19:23:33

标签: java

我正在使用netbeans并继续收到以下错误:

  

类接口或枚举期望的java

RainFall是公共的,应该在名为RainFall.java

的文件中声明

这是代码:

package rainfalldemo;

/**
 *
 * @author 
 */

    import java.util.Scanner;
    import java.text.DecimalFormat;

    public class RainFallDemo {

    /**
     * begin main method
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // declare variables
        final int MAX_MONTHS = 12;
        double totalRainfall;
        double averageRainfall;
        int monthOfMostRain;
        int monthOfLeastRain;

        //create an array of rainfall
        double[] rainfallInYear = new double[MAX_MONTHS];

        //create array of months in a yeat
        String months[] = {"January", "February", "March", "April", "May",   
       "June", "July", "August", "September", "October", "November", 
       "December"};

        //call getRainfallValues method
        getRainfallValues(rainfallInYear, months);

        // creaet rainfall class object
        RainFall rainInYear = new RainFall(rainfallInYear);

        // create an object for DecimalFormat class
        DecimalFormat decimals = new DecimalFormat("0.00");

        //display total rainfall in the year
        totalRainfall = rainInYear.getTotalRainfall();
        System.out.println("The total rainfall in the year is: " + 
        totalRainfall);

        //display average monthly rainfall
        averageRainfall = rainInYear.getAverageRainfall();
        System.out.println("The average monthly rainfall is: " + 
        decimals.format(averageRainfall));

        //display the month with most rain
        monthOfMostRain = rainInYear.getMonthOfMostRainfall();
        System.out.println("The month with the most rain: " + 
        rainfallInYear[monthOfMostRain] + " in " + months[monthOfMostRain]);

        //display month with least rain
        monthOfLeastRain = rainInYear.getMonthOfLeastRainfall();
        System.out.println("The month with the least rain: " + 
        rainfallInYear[monthOfLeastRain] + " in " + months[monthOfLeastRain]);

        }//end of main method

        //getRainfallValues method implementation
        public static void getRainfallValues(double[]rainfallInEachMonth, 
        String[]    monthNames)
        {
        //create an object for Scanner class
        Scanner input = new Scanner(System.in);

        // get rain for every month
        for(int i = 0; i < rainfallInEachMonth.length; i++)
        {
            //prompt user for rainfall in a month
            System.out.print("enter the total rainfall for the month " + 
            monthNames[i] + ": ");
            rainfallInEachMonth[i] = input.nextDouble();
            /* verify whether the rain in the month is negative */
                    while(rainfallInEachMonth[i] < 0)
                    {
                        System.out.print("Enter positive value for rainfall: ");
                        rainfallInEachMonth[i] = input.nextDouble();
                    } //end while
        } //end for
        System.out.println();
    } //end of getRainfallValues method

    }//end of RainfallDemo class

RainFall.java

    //RainFall class

    public class RainFall
    {
    // variable declaration
    private double[] rainInYear;

    public RainFall(double[] rainfallInEachMonth)
    {
        rainInYear = new double[rainfallInEachMonth.length];

        //store rainfall
        for(int  i = 0; i < rainfallInEachMonth.length; i++)
            rainInYear[i] = rainfallInEachMonth[i];
    } // end of parameterized constructor

      //get TotalRainfall method implementation
    public double getTotalRainfall()
    {
        //local variable 
        double totalRainfall = 0.0;
        //calculate the total rainfall in the year
        for(int i= 0; i < rainInYear.length; i++)
            totalRainfall += rainInYear[i];
        return totalRainfall;
    } // end getTotalRainfall method

    //getAverageRainFall method implementation
    public double getAverageRainfall()
    {
        //local variable
        double averageRainfall = 0.0;

        //calculate the average rainfall in a year
        averageRainfall = getTotalRainfall() / rainInYear.length;
        //return averageRainfall
        return averageRainfall;
    } // end of getTotalRainfall method

    //getMonthOfMostRainfall method implementation
    public int getMonthOfMostRainfall()
    {
        // local variable
        double most;
        int monthNumber;
        //initialize variables

        most = rainInYear[0];
        monthNumber = 0;
        //loop repeats to find the month of most rainfall
        for(int i = 1; i < rainInYear.length; i++)
        {
            // find the most rainfall
            if(rainInYear[i] > most)
            {
                most = rainInYear[i];
                monthNumber = i;
            }//end if 
        }  //end for loop
     //return month of most rainfall
        return monthNumber;
    } //end of getMonthOfMostRainfall method

    //getMonthOfLeastRainfall method implementation
    public int getMonthOfLeastRainfall()
    {
        //local variable
        double least;
        int monthNumber;
        //initialize the variables

        least = rainInYear[0];
        monthNumber = 0;
        //loop repeats to find the month of least rainfall
        for(int i = 1; i < rainInYear.length; i++)
        {
            //find least rainfall month
            if(rainInYear[i] < least)
            {
                least = rainInYear[i];
                monthNumber = i;
            }// end if
        } // end for loop
        // return the month of least rainfall
        return monthNumber;
    }// end of getMonthOfLeastRainfall method

} // end of RainFall class

1 个答案:

答案 0 :(得分:1)

你不能在一个文件中拥有多个公共类。把它拿出来并把它放在一个错误的建议文件中,你会很好。

来自Documentation

  

此限制意味着每个最多必须有一个这样的类型   编译单位。这种限制使Java编译器变得容易   在包中查找命名类。在实践中,许多程序员   选择将每个类或接口类型放在自己的编译中   单位,无论是公开的还是由其他代码引用的   编制单位。

另见