我写了一个关于计算一年下雨平均值的程序,给我最高和最低的数量,但是我收到了一个错误,我无法修复它。
这是我的主要代码:`
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 ();
}
}
}
}`
我得到了错误的最低和最高错误,程序必须显示哪个月的输出中最低和最高,但我无法弄清楚代码!!
答案 0 :(得分:4)
删除if行末尾的分号。这导致if在为真时执行空语句。然后无论如何都会执行下一行。