我为此使用命令行参数:
public class Pg253E4 {
public static void main(String[]args) {
try {
System.out.println(Average.average(args)); // This part is saying I don't have a method in Average called average(String[] argName)
} catch(NullPointerException e) {
System.out.println(e.toString());
} catch(NumberFormatException e) {
System.out.println(e.toString());
} finally {
System.out.println("Nice job");
}
}
}
这是另一个类:
public class Average {
public static double average(String[] arrayString) throws NullPointerException,NumberFormatException {
double sum=0;
double arrayStorage[] = new double[arrayString.length];
for(int i = 0; i< arrayString.length; i++) {
arrayStorage[i] = Double.parseDouble(arrayString[i]);
}
for(int i = 0; i<arrayString.length; i++) {
sum += arrayStorage[i];
}
return (sum/arrayString.length);
}
}
我不明白为什么会弹出一个语法错误,说我没有声明或定义类中调用average(String[] argName)
的任何方法Average
,而我显然这样做了。