import java.util.HashMap;
import java.util.Scanner;
public class ex3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
HashMap<String, Integer> nations = new HashMap<String, Integer>();
System.out.println("나라 이름과 인구를 입력하세요.(예: Korea 5000)");
while(true) {
System.out.print("나라 이름, 인구 >>");
String name = sc.next();
if(name.equals("그만")) break;
int population = sc.nextInt();
nations.put(name,population);
}
while(true) {
System.out.print("인구 검색>>");
String search = sc.next();
if(search.equals("그만")) break;
Integer result = -1;
result = nations.get(search);
if(result == null) {
System.out.println(search+" 나라는 없습니다.");
}
else {
System.out.println(search+"의 인구는 "+result);
}
}
}
}
결과
나라 이름과 인구를 입력하세요.(예: Korea 5000)
나라 이름, 인구 >>Korea 5000
나라 이름, 인구 >>USA 1000000
나라 이름, 인구 >>Swiss 2000
나라 이름, 인구 >>France 3000
나라 이름, 인구 >>그만
인구 검색>>France
France의 인구는 3000
인구 검색>>스위스
스위스 나라는 없습니다.
인구 검색>>그만
'명품JAVA프로그래밍 > 7장 제너릭과 컬렉션' 카테고리의 다른 글
[명품JAVA프로그래밍] 7장 실습문제 5-2번 (0) | 2022.02.17 |
---|---|
[명품JAVA프로그래밍] 7장 실습문제 5-1번 (0) | 2022.02.17 |
[명품JAVA프로그래밍] 7장 실습문제 4번 (0) | 2022.02.17 |
[명품JAVA프로그래밍] 7장 실습문제 2번 (0) | 2022.02.17 |
[명품JAVA프로그래밍] 7장 실습문제 1번 (0) | 2022.02.17 |