この例では、入力されたすべての国と大陸とそれに関連する母集団を考慮する必要がある方法があります。背景のために、これはtxt文書の中の(国名、大陸、母集団)などの形で入力されるので、何/何大陸を使うのか分かりません。他のメソッドでは、これらのエントリは変数countryName、countryContinent、およびcountryPopulationに分割されます。この方法は、大陸が使用されているどのように多くの把握、すべてのエントリをループする必要があり、その後、大陸の総人口を計算変数をループして最大値を決定する
(同じ大陸からの5つの異なる国を入力するかもしれませんが、覚えておいてください)
私は入力された大陸の総人口を計算し、これらを各大陸の別々のリストに格納する方法があります。ここから、リストの価値を比較してどの大陸が最も人口が多いのかを調べるにはどうすればよいですか?
public String findMostPopulousContinent(){
int asiaList = 0;
int africaList = 0;
int northAList = 0;
int southAList = 0;
int antList = 0;
int euroList = 0;
int austList = 0;
int highestPop = 0;
for (int n > 0, n < countryContinent.length; n++;)
if (countryName[n].equals("Asia")){
asiaList = asiaList + countryPopulation[n];
}
if (countryName[n].equals("Africa")){
africaList = africaList + countryPopulation[n];
}
if (countryName[n].equals("North America")){
northAList = northAList + countryPopulation[n];
}
if (countryName[n].equals("South America")){
southAList = southAList + countryPopulation[n];
}
if (countryName[n].equals("Antarctica")){
antList = antList + countryPopulation[n];
}
if (countryName[n].equals("Europe")){
euroList = euroList + countryPopulation[n];
}
if (countryName[n].equals("Australia")){
austList = austList + countryPopulation[n];
}
highestpop = Math.max(asiaList, africaList, northAList, southAList, antList, euroLost, austList);
}
ここから、最高のポップスをとり、それを大陸と再関連付けしますか?最高の人口は私に最高の人口を持つintを与えるべきですが、どのリストが最高であるかを知る方法があり、その後、本質的に「[大陸]は[最高人口]で最も人口が多い」
問題の一部は私ですどれくらいの大陸と国が使用されるのか分かりません。私は3つの大陸しか必要としないかもしれない、私は7つすべてを必要とするかもしれない。 if
文
if (countryName[n].equals("Asia")){
asiaList = asiaList + countryPopulation[n];
if (asiaList > highestpop) {
highestpop = asiaList;
biggestContinent = "Asia";
}
}
に
[最小限に再現可能なコードの抜粋]を投稿してくださいは、(HTTP ://stackoverflow.com/help/mcve) – Turtle