2015-09-04 56 views
3

我必须编写一个程序,允许用户跟踪他访问过的所有国家,以及使用2个列表列表的国家和首都的国家和首都:国家和首都。用户可以有三种选项可供选择的菜单,他可以选择:2 ArrayList中的Java数组

  1. 分别添加一个国家及其相应的资本在国家和首都的ArrayList。

  2. 通过输入国家的名称来查询系统的国家首都。 (如果访问了国家,首都应该显示,否则他应该被给一个错误信息:“你没有访问这个国家”)。

  3. 退出程序

    例如ArrayList的国家包含[“英格兰”,“法国”,“团圆”,“尼泊尔”]和一个用于首都含有[“伦敦”,“巴黎”, “圣丹尼斯”,“加德满都”]。如果用户访问了首都内罗毕的肯尼亚,并希望将其添加到数据列表中,则国家和首都数据列表应该变为:[“英格兰”,“法国”,“留尼旺”,“尼泊尔”,“肯尼亚”]和首都分别载有[“伦敦”,“巴黎”,“圣丹尼斯”,“加德满都”,“内罗毕”]。如果他想问法国首都,系统应该显示“巴黎”。如果用户希望寻找澳大利亚的首都 - 系统应显示“您未访问该国家”。

所以这是我想出迄今:

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的,但列出

+1

除了您的主要问题,选择一个地图,其中字符串是关键和arraylist是值。 –

+0

您对资本的检查区分大小写,因此您必须输入考虑到的搜索文本。 –

+0

此外,打印出“国家是...”后,您需要中断;否则您将列出所有国家/地区。 –

回答

2

哦,男孩。这里有很多问题。

首先:您的代码与for循环没有问题(与您称之为问题相关)。

你主要的方法只是执行一个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<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)); 
    } 
} 
+0

非常感谢!我用了你的想法,但仍保持了两个'for'循环和添加'如果(search.equals(ctry.get(I))){ \t \t \t \t \t \t \t的System.out.println( “首都” + cptl.get(J)); \t \t \t \t \t \t \t}' – Tia

+0

@Diksha您的来电,要知道,这不是地道的(即好的做法)在Java中:) –

3

您可以只使用一个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" 

注意如果列表是正确有序的,这只会工作,(所以第一个全国第一资本相匹配,第二个国家第二次增资相匹配,等等)

1

有一个与你问题的方法:有countries with more than one capital

我觉得符合自己需求的良好数据结构将是一个

Map<County,Capital[]> 

地图是非常有用的,该文档是here

奖金愚蠢念头:如果我访问了梵蒂冈统计e,我本来是在罗马,但不在意大利。嗯......如果梵蒂冈城有一个机场,这将是真实的,否则我肯定一直在意大利之前

0
  1. 摆正你的代码while循环
  2. 使用HashMap中,而不是
  3. 梯之前给消息从用户
  4. 输入总是试图从控制台

 

让输入时采取全行输入
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); 
    } 
}