[英]Java arrays in 2 arraylist
我必须编写一个程序,允许用户跟踪他访问过的所有国家,以及他们的首都使用2个arraylists:国家和首都。 用户可以从菜单中选择三个选项,他可以:
分别在Countries和Capital arraylists中添加一个国家及其相应的资本。
通过输入国家/地区名称来查询系统的国家资本。 (如果访问了该国家,则应显示首都,否则应给出错误消息:“您没有访问该国家”)。
退出程序
例如,arraylist国家包含[“英格兰”,“法国”,“留尼旺”,“尼泊尔”]和首都包含[“伦敦”,“巴黎”,“圣丹尼斯”,“加德满都”]。 如果用户访问过肯尼亚,其首都是内罗毕,并希望将其添加到arraylists,则各国和首都的arraylists应成为:[“英格兰”,“法国”,“团聚”,“尼泊尔”,“肯尼亚”]和首都分别包含[“伦敦”,“巴黎”,“圣丹尼斯”,“加德满都”,“内罗毕”]。 如果他想查询法国首都,系统应显示“巴黎”。 如果用户希望寻找澳大利亚首都 - 系统应显示“您没有访问过这个国家”。
所以这就是我到目前为止所得到的:
import java.util.*;
public class MyClass {
public static void main(String[] args) {
ArrayList<String> countries = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
String country;
String capital;
String search;
countries.add("England");
countries.add("France");
countries.add("Reunion");
countries.add("Nepal");
ArrayList<String> capitals = new ArrayList<String>();
capitals.add("London");
capitals.add("Paris");
capitals.add("St Denis");
capitals.add("Kathmandu");
System.out.println("Please choose one option:");
System.out.println("1. Add a new country and its capital.");
System.out.println("2. Search for the capital of a country.");
System.out.println("3. Exit.");
int opt = sc.nextInt();
if (opt == 1) {
country = sc.nextLine();
capital = sc.nextLine();
countries.add(country);
capitals.add(capital);
} else if (opt == 2) {
System.out.println("Enter the capital of the country.");
search = sc.next();
for (int i = 0; i < countries.size(); i++) {
for (int j = 0; j < capitals.size(); j++) {
if (search.equals(capitals.get(j))) {
System.out.println("The country is " + countries.get(i));
}
}
}
} else {
System.exit(0);
}
}
}
但实际上, for
循环显然不起作用,因为当我进入城市的首都时,程序就在那里终止。
编辑:我不能使用HashMap但列表
您可以使用HashMap
,国家/地区为key
,资本为value
:
HashMap<String, String> hashMap = new HashMap<String, String>();
// add data to the HashMap
hashMap.put("England", "London"); //assign value "London" to key "England"
// get data from the HashMap
hashMap.get("England"); //returns "London"
编辑:如果您仍想使用列表,可以执行以下操作以检索相应的值。 这样做的好处是它可以双向工作:
capitals.get(countries.indexOf("England")); //it will return "London"
countries.get(capitals.indexOf("London")); //it will return "England"
请注意,这仅在列表正确排序时才有效(因此第一个国家与第一个国家匹配,第二个国家与第二个国家匹配,等等)
好家伙。 这里有很多问题。
首先:你的代码没有问题(与你所谓的问题有关)与for循环。
main方法只执行一个if分支,然后终止。 如果希望程序在每次完成操作后重新提示菜单,直到请求终止选项,则需要在while循环中包装菜单:
int opt= sc.nextInt();
while (opt != 3) {
if (...)
}
System.exit(0);
第二位:在Java中,您通常不会执行成对的数组(即两个通过其元素的位置进行语义链接的数组)。 您可以创建一个类:
class CountryAndCapital {
String country;
String capital;
//...
}
ArrayListy<CountryAndCapital> myArray = new ArrayList<>();
或者,正如其他人所建议的那样,使用Map,它是将一个数据链接到另一个数据的数据结构(在这种情况下,是国家的资本),并且还防止重复(在某种意义上......):
Map<String, String> countryToCapital = new HashMap<>();
countryToCapital.put("France", "Paris");
//...
最后:如果您的阵列已配对,则无需迭代两者! 您可以遍历国家/地区,只需将该索引转到大写字母:
for(int i=0; i<capitals.size();i++) {
if(search.equals(capitals.get(i))) {
System.out.println("The country is "+countries.get(i));
}
}
while
循环中 HashMap<String, String> countries = new HashMap<>();
Scanner sc = new Scanner(System.in);
String country;
String capital;
String search;
countries.put("England", "London");
countries.put("France", "Paris");
countries.put("Reunion", "St Denis");
countries.put("Nepal", "Kathmandu");
while (true) {
System.out.println("Please choose one option:");
System.out.println("1. Add a new country and its capital.");
System.out.println("2. Search for the capital of a country.");
System.out.println("3. Exit.");
System.out.print("Enter your choice : ");
int opt = Integer.parseInt(sc.nextLine());
if (opt == 1) {
System.out.print("Country : ");
country = sc.nextLine();
System.out.print("Capital : ");
capital = sc.nextLine();
countries.put(country, capital);
} else if (opt == 2) {
System.out.print("Enter the capital of the country : ");
search = sc.nextLine();
for(Map.Entry<String, String> entry : countries.entrySet()){
if(search.equals(entry.getValue())){
System.out.println("The country is " + entry.getKey());
break;
}
}
} else {
System.exit(0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.