2017-02-02 5 views
0

この例では、入力されたすべての国と大陸とそれに関連する母集団を考慮する必要がある方法があります。背景のために、これは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"; 
     } 
} 

+0

[最小限に再現可能なコードの抜粋]を投稿してくださいは、(HTTP ://stackoverflow.com/help/mcve) – Turtle

答えて

1

移動しますmax計算はもちろん、これはおそらくあなたの問題を解決するためにHashMapを使用し

int storedBiggest (int pop, String continent, int highestpop) { 

     if (pop > highestpop) { 
      this.biggestContinent = continent; 
      highestpop = pop; 
     } 
     return highestpop; 
} 
1

のようなものにリファクタリングすることができます。

  • はあなたcountryPopulationList経由のHashMap
  • 反復を作成し、
  • が最大値を持つマップから項目を取得し、マップに置きます。例えばのために

    String countryWithMaxPopulation = Collections.max(countryPopulationMap.entrySet(), (entry1, entry2) -> entry1.getValue() - entry2.getValue()).getKey(); 
    
0

は、Java 8つのストリームを使用する場合、それは任意の定型的なコードなしで、本当に簡単です。

package stackoverflow; 

import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 
import java.util.Map; 
import java.util.stream.Collectors; 

import org.junit.Assert; 
import org.junit.Test; 

public class LoopThroughVariablesToDetermineWhichIsTheLargest { 

    public String findMostPopulousContinent() { 
     //First read countries from disk into a bean. It's easy and better practise to manipulate the data in this way 
     final List<Country> countries = this.readCountriesFromDisk(); 
     //Then we group population by continet 
     final Map<String, Integer> continentPopulation = countries.stream() 
       .collect(Collectors.groupingBy(Country::getContinent, Collectors.summingInt(Country::getPopulation))); 
     //And finally we return the asked result 
     return Collections 
       .max(continentPopulation.entrySet(), (entry1, entry2) -> entry1.getValue() - entry2.getValue()) 
       .getKey(); 
    } 

    @Test 
    public void shouldGetMostPopulousContinentName() throws Exception { 
     Assert.assertEquals("Asia", this.findMostPopulousContinent()); 
    } 

    class Country { 
     private final String name; 
     private final String continent; 
     private final int population; 

     public Country(String name, String continent, int population) { 
      super(); 
      this.name = name; 
      this.continent = continent; 
      this.population = population; 
     } 

     public String getName() { 
      return this.name; 
     } 

     public String getContinent() { 
      return this.continent; 
     } 

     public int getPopulation() { 
      return this.population; 
     } 
    } 

    List<Country> readCountriesFromDisk() { 
     // TODO read from disk 

     // Provide example countries directly for simplicity 
     return Arrays.asList(new Country("Spain", "Europe", 45), new Country("USA", "North America", 325), 
       new Country("USA", "North America", 325), new Country("China", "Asia", 1380), 
       new Country("Brasil", "South America", 207), new Country("India", "Asia", 1331), 
       new Country("Nigeria", "Africa", 191), new Country("Indonesia", "Asia", 260), 
       new Country("Turkey", "Europe", 79), new Country("Iran", "Asia", 80), 
       new Country("Germany", "Europe", 82), new Country("Egypt", "Africa", 92), 
       new Country("Vietnam", "Asia", 93), new Country("USA", "North America", 325)); 
    } 
} 
関連する問題