arrays - Output Error in RainFall Program in java -



arrays - Output Error in RainFall Program in java -

i wrote programme calculating average of raining in year , give me highest , lowest amount getting error , couldn't prepare it.

this main code : `

public class rainfall { //field private double [] rain; public void rainfall (double [] array) { rain = new double [array.length] ; (int i=0; < array.length ; i++) rain [i] = array [i]; } //methods public double gettotal() { double total = 0; ( double r : rain) total +=r; homecoming total; } public double getaverage () { double = gettotal () / rain.length; homecoming a; } public double gethighest () { double highest = rain [0]; (int i=0 ; < rain.length; i++) { if (rain [i] > highest) ; highest = rain [i]; } homecoming highest; } public double getlowest () { double lowest = rain [0]; ( int i=0; <rain.length; i++) { if (rain [i] < lowest) ; lowest = rain [i]; } homecoming lowest; } }

` , 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 " + values.format (rainfalldata.getaverage())); system.out.println("the highest rain " + values.format (rainfalldata.gethighest())); system.out.println("the lowest " + values.format (rainfalldata.getlowest())); } public static void getdata (double [] array) { scanner keyboard = new scanner (system.in) ; ( int i=0 ; < array.length; i++) { system.out.println ("what amont of rain month " + (i + 1) + "?"); array [i] = keyboard.nextdouble (); while (array[i] < 0 ) { system.out.print ( " invalid input. entered negative number. come in a" + "value greater zero"); array [i] = keyboard.nextdouble (); } } } }`

i getting wrong lowest , wrong highest , programme has show month had lowest , highest in output but can't figure out code!!

remove semicolon @ end of if lines. causing if execute empty statement when true. next line executed no matter what.

java arrays

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -