语法错误,为什么我得到它们?

时间:2019-11-06 06:30:32

标签: java arrays

我的Java类遇到了一个问题,涉及获取一年的总量,平均数量,最少数量和最多数量的降雨。我一生无法弄清自己在做什么错。我收到语法错误,但我不太明白为什么。

import java.util.Scanner;
public class Rainfall {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        final int ttlMonth = 12; // for total months , which will always be 12//
        double[] mRain = new double[ttlMonth]; //mRain for monthly rain//
        int i = 0; //for use in the array//
        double ttlRain; //total rain//
        double avgRain; //average rain//
        double maxRain; //most rain//
        double minRain; //least rain//

        System.out.println("Enter the monthly rain fall amount: ");


        for(i = 0; i < ttlMonth; i++){

            do{
                System.out.print("Month " + (i+1) + ": ");
                mRain[i] = scnr.nextDouble();
            }
            while(mRain[i] < 0);            
        }
            //tTl for total//
            ttlRain(double[] array) {
                double tTl = 0; 
                for(i = 0; i < array.length; i++){
                    tTl += array[i];
                    } 
                ttlRain = tTl; 
                }
            //aVg for average//
            avgRain(double[] array) {
                double aVg = 0; 
                aVg = ttlRain(array)/array.length;
            }

            //mAx for the most rainfall//
            maxRain(double[] array) {
                double mAx = 0; 
                for (i = 0; i < array.length; i++){
                    if(array[i] > mAx){
                        mAx = array[i];
                    }
                }

            }           
            //mIn for the least amount of rainfall//
            minRain(double[] array) {
                double mIn = array[0]; 
                for(i = 0; i < array.length; i++){
                    if(array[i] < mIn){
                        mIn = array[i];
                    }
                }

            }
        System.out.println("The yearly sum of rain: " + ttlRain);
        System.out.println("The average ammount of rain: " + avgRain);
        System.out.println("The most amount of rain fall: " + maxRain);
        System.out.println("The least amount of rain fall: " + minRain);
    }                   
}

2 个答案:

答案 0 :(得分:1)

Java语言存在多个问题:

  1. 因为要执行静态函数,所以内部调用的所有函数也必须是静态的

示例,给出:

 static void a(){
    b(); // does not compile
    c(); // compiles
 }

 void b(){}
 static void c(){}
  1. 所有函数都必须声明一个返回类型,例如void或double(在您的情况下)

  2. 所有返回内容的函数都需要带有返回值的return关键字。

  3. 您不能在另一个函数内部创建命名函数,但是可以创建匿名lambda函数

示例,给出:

    double avgRain(double[] array) { // declares a double return type
        return 9.0;
    }
    void print(double in) { // does not return anything. This is a voif function
        System.out.println(in)
    }

5。为了调用函数,您需要传递所有必需的参数

示例:

  double sum(double a,double  b){
    return a+b;
  }
  sum(); // does not compile
  sum(1.0,4.0); // compiles

对代码进行上述更改后,它便可以编译并运行

public class Rainfall {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        final int ttlMonth = 12; // for total months , which will always be 12//
        double[] mRain = new double[ttlMonth]; //mRain for monthly rain//

        System.out.println("Enter the monthly rain fall amount: ");

        for (int i = 0; i < ttlMonth; i++) {

            do {
                System.out.print("Month " + (i + 1) + ": ");
                mRain[i] = scnr.nextDouble();
            }
            while (mRain[i] < 0);
        }

        System.out.println("The yearly sum of rain: " + ttlRain(mRain));//5
        System.out.println("The average ammount of rain: " + avgRain(mRain));//5
        System.out.println("The most amount of rain fall: " + maxRain(mRain));//5
        System.out.println("The least amount of rain fall: " + minRain(mRain));//5
    }

    //tTl for total//
    private static double ttlRain(double[] array) { // 1,2,3
        double tTl = 0;
        for (int i = 0; i < array.length; i++) {
            tTl += array[i];
        }
        return tTl;//4
    }

    //aVg for average//
    private static double avgRain(double[] array) { // 1,2,3
        return ttlRain(array) / array.length;
    }

    //mAx for the most rainfall//
    private static double maxRain(double[] array) { // 1,2,3
        double mAx = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > mAx) {
                mAx = array[i];
            }
        }
        return mAx;
    }

    //mIn for the least amount of rainfall//
    private static double minRain(double[] array) { // 1,2,3
        double mIn = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] < mIn) {
                mIn = array[i];
            }
        }
        return mIn;
    }
}

答案 1 :(得分:0)

这里有多个问题。您为什么不了解有关Java的更多信息:

  1. Java方法声明语法错误,如下所示

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <link rel="stylesheet" href="style.css">
            <script src="script.js"></script>
        </head>
        <body>
            <div id = "start-menu">
                <button id="start">Start</button>
                <p id ="high-score">Your High-Score: 0</p>
                <script src="script.js"></script>
            </div>
        </body>
    </html>

    例如[public|private|protected|default] [void|return_type] methodname([parameters...])

  2. 在Java中,方法未在另一个方法内直接声明。因此,将您的方法移出main方法之外。并通过创建类的对象使它们静态或调用。