Java数组引用

时间:2014-05-19 13:29:43

标签: java arrays string reference

我需要找到另一种方式来协调阵容中人口较多的城市。 代码是这样的,但有时他会给我正确的城市名称,而且他给我一个错误的城市名称,有人有任何想法吗?

public class Esercizio32_101
{
    public static void main(String[] args)
    {
        // Object for InputStream
        ConsoleReader2 tastiera = new ConsoleReader2(System.in);
        // declarations
        String names[] = new String[5];
        int population[] = new int[5];
        String n = null;
        int higher = 0;
        int total = 0;
        int c = 0;
        // calcoli
        for (int i = 0; i < names.length; i++)
        {
            System.out.print("Insert the name of city N° " + (i + 1) + " ===> ");
            do
            {
                names[i] = tastiera.readLine();
                if (names[i].equals(""))
                {
                    System.out.print("You must insert the city name, try again ===> ");
                }
            }
            while (names[i].equals(""));

        }
        for (int i = 0; i < names.length; i++)
        {
            System.out.print("Insert the population of city N° " + (i + 1) + " ===> ");
            population[i] = tastiera.readInt();
            total = population[i];
            if (total > higher)
            {
                higher = total;
                c++;
            }
        }
        System.out.print("The most populated city is " + names[c]);
    }
}

3 个答案:

答案 0 :(得分:3)

最直接的问题是:

if (total > higher)
{
    higher = total;
    c++;
}

通过递增c,您只需记住有多少城市拥有的人口数量超过前一个城市。您想要记住人口较多城市的索引

if (total > higher)
{
    higher = total;
    c = i;
}

此外,我强烈建议您创建一个City类型来封装&#34;名称和人口&#34;。要求所有城市详细信息填充List<City>City[],然后您就可以找到人口最多的城市。每当您发现自己有多个集合都具有相同的大小时,您应该考虑重构一个集合,其类型为每个原始集合封装一个值。

这种方法鼓励你分开数据收集&#34;来自&#34;数据处理&#34; - 目前,当您要求用户输入时,您会检测到最大的人口。这意味着如果您以后想要处理从磁盘加载数据的情况,您需要重写内容。如果您将不同的问题分开,它将使您的代码更容易修改和测试。

最后,我建议您更仔细地考虑变量名称。 total以何种方式传达了我现在所询问的城市人口的意图&#34;?

答案 1 :(得分:0)

替换

c++;

通过

c = i;

它适用于所有情况。

答案 2 :(得分:0)

只需考虑具有人口最大值的索引的值。只要你需要跟踪人口[i]的最大值的索引,增加c就没有意义。

而是试试这个:

public class Esercizio32_101
{
    public static void main(String[] args)
    {
        // Object for InputStream
        ConsoleReader2 tastiera = new ConsoleReader2(System.in);
        // declarations
        String names[] = new String[5];
        int population[] = new int[5];
        String n = null;
        int higher = 0;
        int total = 0;
        int c = 0;
        // calcoli
        for (int i = 0; i < names.length; i++)
        {
            System.out.print("Insert the name of city N° " + (i + 1) + " ===> ");
            do
            {
                names[i] = tastiera.readLine();
                if (names[i].equals(""))
                {
                    System.out.print("You must insert the city name, try again ===> ");
                }
            }
            while (names[i].equals(""));

        }
        for (int i = 0; i < names.length; i++)
        {
            System.out.print("Insert the population of city N° " + (i + 1) + " ===> ");
            population[i] = tastiera.readInt();
            total = population[i];
            if (total > higher)
            {
                higher = total;
                c=i;//here is the change c should keep track of the higher total of population, if c dosent change then the fist value of the array population[] (i=0) have the biggest value.
            }
        }
        System.out.print("The most populated city is " + names[c]);
    }
}