循环变量以确定哪个是最大的

时间:2017-02-02 01:58:27

标签: java eclipse instance-variables

因此,对于这个例子,我有一个方法需要考虑输入的所有国家和大陆及其相关人口。对于背景,这将在txt文档中以(countryName,continent,population)等形式输入,因此我不知道将使用多少/多少大洲。在其他方法中,这些条目分为变量countryName,countryContinent和countryPopulation。

这种方法需要循环遍历所有条目,计算出使用了多少大陆,然后计算各大洲的总人口(请记住,可能输入来自同一大洲的五个不同国家)

我已经有了计算输入大陆总人口的方法,并将这些方法存储在每个大陆的单独列表中。从这里开始,如何比较列表的值以找出哪个大陆的人口最多?

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);

}

然后从这里开始,获取最高的流行价值,并将其与其大陆重新关联? highpop应该给我一个人口最多的int但有一种方法可以知道哪个列表最高然后基本上返回&#34; [Continent]在[最高人口]中拥有最高人口&#34;

部分问题是我不知道将使用多少大洲和国家。我可能只需要三大洲,我可能需要全部七个。

3 个答案:

答案 0 :(得分:1)

max计算移至if语句

if (countryName[n].equals("Asia")){
        asiaList = asiaList + countryPopulation[n];
        if (asiaList > highestpop) {
            highestpop = asiaList;
            biggestContinent = "Asia";
        }
}

当然这可以重构为像

这样的东西
 int storedBiggest (int pop, String continent, int highestpop) {

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

答案 1 :(得分:1)

可能使用HashMap来解决您的问题。

  • 创建HashMap
  • 遍历您的countryPopulationList并将其放入地图
  • 从地图中获取具有最大值的项目。

    例如:

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

答案 2 :(得分: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));
    }
}