嗨,我的任务快要结束了,但是我似乎无法获得我的输出值来输出每个省的加拿大大城市,这也是该省的省会城市。下面是我的代码,在单独的框中是我的方法代码。提前致谢。
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
您的代码中存在三个问题:
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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。