gpt4 book ai didi

java - 如何从不同的数组中找到与数组的最大编号相关的索引?

转载 作者:行者123 更新时间:2023-12-01 17:38:29 25 4
gpt4 key购买 nike

这就是我目前拥有的代码。首先,当它计算所有值的平均值时,我不断遇到退出代码。我不知道为什么会发生这种情况,因为它之前工作正常,我一直在编辑它以使其恢复到之前的状态,但我不断收到退出代码。

之后,我无法显示数组中的最大值,也无法显示与之相关的城市......如果有人能帮助我那就太好了!

public class Main {

public static void main(String[] args) {

System.out.println("- - - - - - - - - - - - - - - ");
System.out.println("City\t\tPopulation\t\t\t");
System.out.println("- - - - - - - - - - - - - - - ");

int[] population = {
693417,
457502,
109985,
107360,
103773,
13145,
5469
};
String[] cities = {
"Denver",
"Colo Springs",
"Pueblo",
"Boulder",
"Greely",
"Fruita",
"Vail"
};


displayPopulations(population, cities);

int[] total = computeTotalPopulation(population);
for (int i = 0; i < total.length; i++);

double[] average = computeAveragePopulation(population);
for (int i = 0; i < average.length; i++);



}


public static void displayPopulations(int[] population, String[] cities) {

System.out.println(cities[0] + "\t\t" + population[0]);
System.out.println(cities[1] + "\t" + population[1]);
System.out.println(cities[2] + "\t\t" + population[2]);
System.out.println(cities[3] + "\t\t" + population[3]);
System.out.println(cities[4] + "\t\t" + population[4]);
System.out.println(cities[5] + "\t\t" + population[5] + "\n");
}


public static int[] computeTotalPopulation(int[] population) {

int[] population2 = {
693417,
457502,
109985,
107360,
103773,
13145,
5469
};
int sum = 0;
for (int i = 0; i < population2.length; i++) {
sum += population2[i];
}

System.out.println("Total population for all cities is: " + sum);
return population2;

}

public static double[] computeAveragePopulation(int[] population) {

double[] population3 = {
693417,
457502,
109985,
107360,
103773,
13145,
5469
};
double sum = 0;
for (int i = 0; i < population3.length; i++) {
sum = sum + population3[i];
}

double averagePopulation = sum / population3.length;
System.out.printf("Average population for all cities is: %.2f", averagePopulation + "\n");
return population3;

}
}

最佳答案

您应该为computeTotalPopulation方法返回int sum,为computeAveragePopulation方法返回double averagePopulation,而不是返回数组。这是我给你的最大提示,剩下的你必须自己弄清楚。

关于java - 如何从不同的数组中找到与数组的最大编号相关的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61005003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com