简体   繁体   中英

Output Error in RainFall Program in java

I wrote a program about calculating the average of raining in a year and give me the highest and the lowest amount but I am getting an error and I couldn't fix it.

This is my main code : `

public class Rainfall {

//field 
private double [] rain;

public void Rainfall (double [] array) {

    rain = new double [array.length] ; 

    for (int i=0; i < array.length ; i++) 

        rain [i] = array [i];   
}

//Methods
public double getTotal() {

    double total = 0;

    for ( double r : rain)

total +=r;
    return total;

}

public double getAverage () {

    double a = getTotal () / rain.length;

    return a;
}

public double getHighest () { 

    double highest = rain [0];

    for (int i=0 ; i < rain.length; i++) { 

        if (rain [i] > highest) ;
        highest = rain [i];
}
    return highest;         
}

public double getLowest () {

    double lowest = rain [0];

    for ( int i=0; i <rain.length; i++) { 

        if (rain [i] < lowest) ;

        lowest = rain [i];
    }
    return lowest;
}

}

` and this is the demo :

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


  public class RainFallDemo { 

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


        final int year1 = 12;

        double [] rains = new double [year1];

        getData (rains) ;

        Rainfall rainfallData = new Rainfall ();

        DecimalFormat values = new DecimalFormat ("#0.00") ;

        System.out.println("The total rain is" + values.format(rainfallData.getTotal()));
        System.out.println("The average is " + values.format (rainfallData.getAverage()));
        System.out.println("The highest rain is " + values.format (rainfallData.getHighest()));
        System.out.println("The lowest is " + values.format (rainfallData.getLowest()));




    }

    public static void getData (double [] array) {

        Scanner keyboard = new Scanner (System.in) ;

        for ( int i=0 ; i < array.length; i++) 
        {

            System.out.println ("what was the amont of rain for month  " + (i + 1) + "?");

            array [i] = keyboard.nextDouble ();

            while (array[i] < 0 )
            {
                System.out.print ( " Invalid input. You entered a negative number. Enter a" + 
            "value that is greater than zero");
            array [i] = keyboard.nextDouble ();

            }
    }
}
}`

I was getting the wrong lowest and wrong highest and The program has to show which month had the lowest and highest in the output but but I can't figure out the code!!

Remove the semicolon at the end of your if lines. That is causing the if to execute an empty statement when it is true. Then the next line is always executed no matter what.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM