자바 map, hashmap
package test;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, Integer> map = new HashMap<>();
System.out.println("나라이름과 인구를 입력하세요(예: Korea 5000)");
while (true) {
System.out.print("나라이름, 인구 >> ");
String contryName = scanner.next();
if (contryName.equals("그만")) {
break; // 입력 중지
}
int population = scanner.nextInt();
map.put(contryName, population);
}
while (true) {
System.out.print("인구 검색 >> ");
String countryName = scanner.next();
if (countryName.equals("그만")) {
break;
}
Integer population = map.get(countryName);
if (population == null) {
System.out.println(countryName+" 나라는 없습니다.");
} else {
System.out.println(countryName+"의 인구는 "+ population);
}
} // while
scanner.close();
} // main()의 끝
}