• 나라와 수도를 입력하고 퀴즈를 맞추는 CapitalTest클래스 만들기.

    • 메뉴선택, 입력 메소드, 퀴즈 메소드, 파일 저장 메소드 4개의 메소드를 구현한다.

    • 파일 저장은 FileWriter 클래스를 사용한다.

    • 랜덤변수 생성은 Math.random()메소드를 사용한다.

 

  • CapitalTest 클래스 (CapitalTest)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.exam02;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class CapitalTest {
    public static Scanner sc=new Scanner(System.in);
    private Map<StringString>map=new HashMap<StringString>();
    File dir, file;
    public CapitalTest() {
        dir=new File("src\\com\\exam02");
        file=new File(dir,"CapitalTest.txt");
        map.clear();
        try {
        if(!file.exists()) {
            file.createNewFile();
            return;
        }
        Scanner scanner=new Scanner(file);
        while(scanner.hasNext()) {
            String country=scanner.next();
            String capital=scanner.next();
            map.put(country, capital);
        }
        scanner.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void showMenu() {
        System.out.println("****수도 맞추기 게임을 시작합니다.****");
        System.out.println("1.입력, 2.퀴즈, 3.파일저장 및 종료");
        System.out.println("선택>>");
    }
    
    public void input() {
        System.out.println("현재"+map.size()+" 개 나라와 수도 입력");
        while(true) {
            System.out.println("나라와 수도 입력(종료는 x)");
            String cont=sc.next();
            if(cont.toUpperCase().equals("X")) break;
            if(map.containsKey(cont)==true) {
                System.out.println("이미 입력한 나라입니다.");
                continue;
            }
            String cap=sc.next();
            map.put(cont, cap);
        }
    }
    
    public void quiz() {
        Set<String > set=map.keySet();
        Object[]arr=set.toArray();
        while(true) {
            int n=(int)(Math.random()*map.size());
            String city="";
            try{
                city=(String)arr[n];
            }catch(ArrayIndexOutOfBoundsException e) {
                System.out.println("map이 비어 있습니다.");
                return;
            }
            String dosi=map.get(city);
            System.out.println(city+" 의 수도는? 종료는 x");
            String dap=sc.next();
            if(dap.toLowerCase().equals("x")) {
                System.out.println("종료합니다.");
                break;
            }
            if(dap.equals(dosi)) {
                System.out.println("정답");
            }
            else {
                System.out.println("틀렸습니다.");
            }
        }
    }
    
    public void save() {
        FileWriter fw=null;
        try {
            fw=new FileWriter(file);
            Set<String> set=map.keySet();
            Iterator<String>it=set.iterator();
            while(it.hasNext()) {
                String key=it.next();
                String value=map.get(key);
                fw.write(key+" ");
                fw.write(value+"\n");
            }
            fw.close();
            System.out.println("종료");
            System.exit(0);
        }catch (IOException x) {
            x.printStackTrace();
            System.out.println("파일저장 오류");
        }
    }
    
    public static void main(String[] args) {
        CapitalTest ca=new CapitalTest();
        while(true) {
            CapitalTest.showMenu();
            int choice=CapitalTest.sc.nextInt();
            switch(choice) {
            case 1 : ca.input(); break;
            case 2 : ca.quiz(); break;
            case 3 : ca.save(); break;
            default : System.out.println("입력오류");          
            }
        }
    }
}

+ Recent posts