嗨,我的任务快要结束了,但是我似乎无法获得我的输出值来输出每个省的加拿大大城市,这也是该省的省会城市。下面是我的代码,在单独的框中是我的方法代码。提前致谢。
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;
}
对方法进行了一些编辑,现在它只返回我输入的内容...
最佳答案
您的代码中存在三个问题:
您正在使用city == provinces[i][1]
和city == provinces[i][2]
比较字符串,这不是比较字符串的正确方法。请注意,==
仅比较引用。不是字符串的内容。
您已将break
语句放在if
块之外(当找到匹配的true
时,该语句将对city
求值)。因此,for
循环仅执行一次,而与数组中的元素provinces
的数量以及是否找到匹配项无关。
如果我正确理解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]
关于java - 尝试在if函数JAVA中获得相同值的2d数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60689947/