开发者社区> 问答> 正文

尝试在if函数JAVA中获得相同值的2d数组

嗨,我的任务快要结束了,但是我似乎无法获得我的输出值来输出每个省的加拿大大城市,这也是该省的省会城市。下面是我的代码,在单独的框中是我的方法代码。提前致谢。

public class Canada
{
    public static final int BC = 0;
    public static final int AB = 1;
    public static final int ON = 2;
    public static final int QC = 3;
    public static final int NL = 4;
    public static final int PE = 5;
    public static final int NS = 6;
    public static final int NB = 7;
    public static final int MB = 8;
    public static final int SK = 9;
    public static final int NU = 10;
    public static final int YT = 11;
    public static final int NT = 12;

    public static final int NAME_OF_PROVINCE = 0;
    public static final int NAME_OF_CAPITAL_CITY = 1;
    public static final int NAME_OF_BIGGEST_CITY = 2;

    public String[][] provinces;
    public Canada()
    {
        provinces = new String[13][3];

        provinces[BC][NAME_OF_PROVINCE] = "british columbia";
        provinces[BC][NAME_OF_CAPITAL_CITY] ="victoria";
        provinces[BC][NAME_OF_BIGGEST_CITY] ="vancouver";

        provinces[AB][NAME_OF_PROVINCE] = "alberta";
        provinces[AB][NAME_OF_CAPITAL_CITY] ="edmonton";
        provinces[AB][NAME_OF_BIGGEST_CITY] ="calgary";

        provinces[ON][NAME_OF_PROVINCE] = "ontario";
        provinces[ON][NAME_OF_CAPITAL_CITY] ="toronto";
        provinces[ON][NAME_OF_BIGGEST_CITY] ="toronto";

        provinces[QC][NAME_OF_PROVINCE] = "quebec";
        provinces[QC][NAME_OF_CAPITAL_CITY] ="quebec city";
        provinces[QC][NAME_OF_BIGGEST_CITY] ="montreal";

        provinces[NL][NAME_OF_PROVINCE] = "newfoundland";
        provinces[NL][NAME_OF_CAPITAL_CITY] ="st johns";
        provinces[NL][NAME_OF_BIGGEST_CITY] ="st johns";

        provinces[PE][NAME_OF_PROVINCE] = "prince edward island";
        provinces[PE][NAME_OF_CAPITAL_CITY] ="charlottetown";
        provinces[PE][NAME_OF_BIGGEST_CITY] ="charlottetown";

        provinces[NS][NAME_OF_PROVINCE] = "nova scotia";
        provinces[NS][NAME_OF_CAPITAL_CITY] ="halifax";
        provinces[NS][NAME_OF_BIGGEST_CITY] ="halifax";

        provinces[NB][NAME_OF_PROVINCE] = "new brunswick";
        provinces[NB][NAME_OF_CAPITAL_CITY] ="fredericton";
        provinces[NB][NAME_OF_BIGGEST_CITY] ="saint john";

        provinces[MB][NAME_OF_PROVINCE] = "manitoba";
        provinces[MB][NAME_OF_CAPITAL_CITY] ="winnipeg";
        provinces[MB][NAME_OF_BIGGEST_CITY] ="winnipeg";

        provinces[SK][NAME_OF_PROVINCE] = "saskatchewan";
        provinces[SK][NAME_OF_CAPITAL_CITY] ="regina";
        provinces[SK][NAME_OF_BIGGEST_CITY] ="saskatoon";

        provinces[NU][NAME_OF_PROVINCE] = "nunavut";
        provinces[NU][NAME_OF_CAPITAL_CITY] ="iqaluit";
        provinces[NU][NAME_OF_BIGGEST_CITY] ="iqaluit";

        provinces[YT][NAME_OF_PROVINCE] = "yukon";
        provinces[YT][NAME_OF_CAPITAL_CITY] ="whitehorse";
        provinces[YT][NAME_OF_BIGGEST_CITY] ="whitehorse";

        provinces[NT][NAME_OF_PROVINCE] = "northwest territories";
        provinces[NT][NAME_OF_CAPITAL_CITY] ="yellowknife";
        provinces[NT][NAME_OF_BIGGEST_CITY] ="yellowknife";
    }
}

下面是我尝试在省份中与省会城市中的大城市进行输出的方法代码。像多伦多一样,是安大略省的省会城市,也是安大略省最大的城市。温哥华是不列颠哥伦比亚省最大的城市,但不是首府城市。我尝试了各种各样的组合,这些组合要么总是不输出计算结果,要么只输出我键入的城市。

ps我知道2D数组代码很糟糕,但这是老师设置此问题的初始版本的方式。

public String getProvinceWhoseCapitalsIs(String city) // return
{
    String sameCity = "not computing";
    for(int i = 0;i<provinces.length;i++)
    {
        if(city == null || city.isEmpty())
        {
            throw new IllegalArgumentException("Please enter correct province name");
        }
        if(city == provinces[i][1] && city == provinces[i][2])
        {
            city = provinces[i][1];

        }
        break;
    }
    return city;
}

对方法进行了一些编辑,现在它只返回我输入的内容...

问题来源:Stack Overflow

展开
收起
montos 2020-03-25 15:48:16 369 0
1 条回答
写回答
取消 提交回答
  • 您的代码中存在三个问题:

    1. 您正在使用city == provinces[i][1]和city == provinces[i][2]比较字符串,这不是比较字符串的正确方法。注意,==仅比较参考;不是字符串的内容。
    2. 您已将break语句放在if块外(预计将true在city找到匹配项时对其进行评估)。因此,for无论数组中的元素数量如何,provinces以及是否找到匹配项,循环仅执行一次。
    3. 如果我getProvinceWhoseCapitalsIs正确理解了目的,应该返回省名而不是城市名。因此,您应该返回provinces[i][0]而不是provinces[i][1]。 如果您的方法不打算执行上述操作,则可以忽略上述第三点。 如下进行:
    public String getProvinceWhoseCapitalsIs(String city) {
        String province = "";
        if (city == null || city.isEmpty()) {
            throw new IllegalArgumentException("Please enter correct province name");
        }
        for (int i = 0; i < provinces.length; i++) {
            if (city.equalsIgnoreCase(provinces[i][1])) {
                province = provinces[i][0];
                break;
            }
        }
        return province;
    }
    

    发布此更新以编写以下测试代码:

    public class Main {
        public static void main(String[] argv) {
            Canada canada = new Canada();
            System.out.println(canada.getProvinceWhoseCapitalsIs("toronto"));
        }
    }
    

    输出:

    ontario
    

    根据您的评论,我了解您正在寻找一种获取最大首都城市列表的方法。为了满足此要求,您需要将以下方法放入class Canada:

    public List<String> getBiggestCapitalCities() {
        List<String> cities = new ArrayList<String>();
        for (int i = 0; i < provinces.length; i++) {
            if (provinces[i][1].equalsIgnoreCase(provinces[i][2])) {
                cities.add(provinces[i][1]);
            }
        }
        return cities;
    }
    

    测试代码:

    public class Main {
        public static void main(String[] argv) {
            Canada canada = new Canada();
            System.out.println(canada.getBiggestCapitalCities());
        }
    }
    

    输出:

    [toronto, st johns, charlottetown, halifax, winnipeg, iqaluit, whitehorse, yellowknife]
    

    回答来源:Stack Overflow

    2020-03-25 15:51:36
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载