java中Rainsion程序中的数组输出错误
我写了一个关于计算一年平均降雨量的程序,给了我最高和最低的降雨量,但是我得到了一个错误,我无法修复它
这是我的主要代码:`
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;
}
}
` 这是演示:
`
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 ();
}
}
}
}`
我得到了错误的最低值和错误的最高值,程序必须显示哪个月的输出值是最低和最高的,但我无法找出代码
# 1 楼答案
删除if行末尾的分号。这会导致if在为true时执行空语句。那么不管发生什么,下一行总是被执行