[英]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.