Java数组引用

Java数组引用,java,arrays,string,reference,Java,Arrays,String,Reference,我需要找到另一种方法与阵列中人口较多的城市进行通信。 代码是这样的,但有时他给我正确的城市名称,有时他给我错误的城市名称,有人知道吗 public class Esercizio32_101 { public static void main(String[] args) { // Object for InputStream ConsoleReader2 tastiera = new ConsoleReader2(System.in);

我需要找到另一种方法与阵列中人口较多的城市进行通信。 代码是这样的,但有时他给我正确的城市名称,有时他给我错误的城市名称,有人知道吗

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]);
    }
}
公共类Esercizio32_101
{
公共静态void main(字符串[]args)
{
//输入流的对象
ConsoleReader2 tastiera=新的ConsoleReader2(System.in);
//声明
字符串名称[]=新字符串[5];
整数人口[]=新整数[5];
字符串n=null;
int更高=0;
int-total=0;
int c=0;
//卡尔科利
for(int i=0;i”;
做
{
names[i]=tastiera.readLine();
if(名称[i].等于(“”)
{
System.out.print(“您必须插入城市名称,重试==>”;
}
}
while(名称[i]。等于(“”);
}
for(int i=0;i”;
人口[i]=tastiera.readInt();
总数=人口[i];
如果(总数>更高)
{
更高=总数;
C++;
}
}
系统输出打印(“人口最多的城市是”+名称[c]);
}
}
更换

c++;


它在任何情况下都会起作用。

最紧迫的问题是:

if (total > higher)
{
    higher = total;
    c++;
}
通过增加
c
,您只需记住有多少城市的人口比之前的任何城市都多。您想记住人口较多的城市的指数:

if (total > higher)
{
    higher = total;
    c = i;
}
此外,我强烈建议您创建一个
City
类型来封装“名称和人口”。要求提供所有城市详细信息以填充
列表
城市[]
,然后您可以找到人口最多的城市。任何时候你发现你拥有多个集合,这些集合都具有相同的大小,你应该考虑重构到一个集合,该集合包含一个值,每个原始集合都封装了一个值。 这种方法还将鼓励您将“数据收集”与“数据处理”分开——目前,您在请求用户输入时检测到的人口最多。这意味着,如果您以后想处理从磁盘加载数据的情况,您需要重写这些内容。如果您分离出不同的关注点,那么您的代码将更易于修改和测试


最后,我建议您更仔细地考虑变量名。
total
以何种方式传达了“我目前所询问的城市人口”的意图?

只需考虑人口最大值的指数值即可。只要您需要跟踪总体[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]);
    }
}
公共类Esercizio32_101
{
公共静态void main(字符串[]args)
{
//输入流的对象
ConsoleReader2 tastiera=新的ConsoleReader2(System.in);
//声明
字符串名称[]=新字符串[5];
整数人口[]=新整数[5];
字符串n=null;
int更高=0;
int-total=0;
int c=0;
//卡尔科利
for(int i=0;i”;
做
{
names[i]=tastiera.readLine();
if(名称[i].等于(“”)
{
System.out.print(“您必须插入城市名称,重试==>”;
}
}
while(名称[i]。等于(“”);
}
for(int i=0;i”;
人口[i]=tastiera.readInt();
总数=人口[i];
如果(总数>更高)
{
更高=总数;
c=i;//这里是变化,c应该跟踪较高的总体,如果c发生变化,则数组总体[](i=0)的第一个值具有最大值。
}
}
系统输出打印(“人口最多的城市是”+名称[c]);
}
}

您应该使用
名称
人口
属性创建一个
城市
类型,而不是保留两个单独的数组。我还建议创建一个单独的方法,该方法接受城市实例的数组或列表,并返回人口最多的实例。将所有I/O排除在该方法之外。你可以单独测试它,然后把它放在一边。只需转储完整的代码,而不显示更改的内容或更改的原因(这一点更为重要)并没有多大帮助。第一句话对我来说没有多大意义。。。“不必增加的最后一个最大索引”?
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]);
    }
}