Java calHighestAvg(cityRain)的创建方法
如何执行calHighestAvg的代码Java calHighestAvg(cityRain)的创建方法,java,Java,如何执行calHighestAvg的代码 public class City { private String cityName; private double[] monhtlyRain = new double[12]; //constructor //processor to return the average of the current year public double yearlyRainAvg() public
public class City {
private String cityName;
private double[] monhtlyRain = new double[12];
//constructor
//processor to return the average of the current year
public double yearlyRainAvg()
public double calHighestAvg()
//getters
//toString
}
public class cityRainApp {
public static void main(String[] args) {
City[] cityRain = new City[20];
//input
System.out.println("The city with the highest of rain for this year :");
City highestCity = calHighestAvg(cityRain); //can't understand this
System.out.println(highestCity.toString());
}
}
我找不到为calHighestAvgcityRain执行方法的方法您对calHighestAvg的调用永远不会按原样工作-因为您的City类中有一个方法,并且您正试图从主cityRainApp类中调用另一个同名的未编写方法。一个返回一个双人房间,另一个应该返回一个城市 实现这一点的方法是实现您未编写的calHighestAvg方法更改您的方法名称以减少混淆!!返回cityRainApp类中主程序中的城市。更改名称后,在cityRainApp类中,其外观应如下所示:
public City highestAvg(City[] cityRain) {
double highestRain = cityRain[0].calHighestAvg();
City highestRainCity = cityRain[0];
for (int i = 0; i < cityRain.length; ++i) {
double currAvg = cityRain[i].calHighestAvg();
if (currAvg > highestRain) {
highestRain = currAvg;
highestRainCity = cityRain[i]
}
}
return highestRainCity;
}
使用StackOverflow拥有的代码格式化工具,它非常有帮助。还要研究如何使用循环。祝你作业顺利。获取数据,然后计算平均值?你具体对什么有意见?这里没有人会给你答案。