본문 바로가기
JAVA

영어 단어 테스트 프로그램 만들기

by Big Sun 2022. 4. 3.
728x90

첫 번째 시도!

package ex;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;

class Word {
	String englishWord;
	String koreaWord;
	public Word(String englishWord, String koreaWord) {
		this.englishWord = englishWord;
		this.koreaWord = koreaWord;
	}
}
public class EnglishTest {

	static Vector<Word> v = new Vector<Word>();
	
	public static void savedWord() {
		// 사용자가 사용하기전 저장된 단어들
		v.add(new Word("love","사랑"));
		v.add(new Word("notice","공지,주의"));
		v.add(new Word("warranty","보증(서)"));
		v.add(new Word("profit","이익"));
		v.add(new Word("opening","공석"));
		v.add(new Word("estimate","견젹"));
		v.add(new Word("branch","지사"));
		v.add(new Word("policy","정책"));
		v.add(new Word("position","직책"));
		v.add(new Word("department","부서"));
		v.add(new Word("client","고객"));
		v.add(new Word("supplier","공급업체"));
		v.add(new Word("rate","요금"));
		v.add(new Word("warehouse","창고"));
		v.add(new Word("route","길,노선"));
		v.add(new Word("conservative","보수적인"));
		v.add(new Word("so as to","~하기 위해서"));
		v.add(new Word("unanimously","만장일치로"));
		v.add(new Word("proceeds","수익금"));
		v.add(new Word("be liable for","~에 대한 책임이 있다"));
		v.add(new Word("issue","발표하다"));
	}
	
	public static void main(String[] args) {
		
		int choice= 0;
		String answer ="";
		savedWord();
		Scanner scan = new Scanner(System.in);
		HashMap<String,String> map = new HashMap<String,String>();
		Iterator<Word> iter = v.iterator();
		while(iter.hasNext()) {
			Word word = iter.next();
			map.put(word.englishWord, word.koreaWord);
		}
		
		Set<String> set = map.keySet();
		Iterator<String> iter2 = set.iterator();
		while(iter2.hasNext()) {
			
			
			if(choice != -1) {
				
				System.out.println("\"영단어 테스트\" 시작합니다. (-1을 입력하면 종료합니다.) ");
				System.out.println("현재" + v.size() +"의 단어가 들어 있습니다.");
				String question = iter2.next();
				String[] fakeAnswer = new String[4];
				int random = (int)(Math.random() * 4);
				for(int i=0;i<4;i++) {
					if(i == random) {
							answer	= map.get(question);
							fakeAnswer[i] = answer;
					}else {
						fakeAnswer[i] = map.get(iter2.next());
						if(fakeAnswer[i].equals(answer)) {
							i--;
						}
					}
					
				}
				System.out.println(question + "?");
				System.out.println("(1)" + fakeAnswer[0] + "(2)" + fakeAnswer[1] + "(3)" + fakeAnswer[2] + "(4)" + fakeAnswer[3]);
				choice = scan.nextInt();
				switch(choice) {
				case 1:
					if(map.get(question).equals(fakeAnswer[0])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + map.get(question));
					}
					break;
				case 2:
					if(map.get(question).equals(fakeAnswer[1])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + map.get(question));
					}
					break;
				case 3:
					if(map.get(question).equals(fakeAnswer[2])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + map.get(question));
					}
					break;
				case 4:
					if(map.get(question).equals(fakeAnswer[3])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + map.get(question));
					}
					break;
				}
				
			}else {
				System.out.println("\"영단어 테스트\" 를 종료합니다.");
				System.exit(0);
			}
			
			
		}
	}

}

잘 돌아 가는 데 HashMap의 특성때문에 Iterator때문에 반복되는 횟수가 한정 되어있다.

몇 번 돌리면 자동적으로 iter.hasNext()가 없기때문에 오류가 난다.

 

그래서 Vector와 Math.random()함수만 이용해서 작성해보겠습니다.

 

 

 

package ex;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;

class Word {
	String englishWord;
	String koreaWord;
	public Word(String englishWord, String koreaWord) {
		this.englishWord = englishWord;
		this.koreaWord = koreaWord;
	}
}
public class EnglishTest {

	static Vector<Word> v = new Vector<Word>();
	
	public static void savedWord() {
		// 사용자가 사용하기전 저장된 단어들
		v.add(new Word("love","사랑"));
		v.add(new Word("notice","공지,주의"));
		v.add(new Word("warranty","보증(서)"));
		v.add(new Word("profit","이익"));
		v.add(new Word("opening","공석"));
		v.add(new Word("estimate","견젹"));
		v.add(new Word("branch","지사"));
		v.add(new Word("policy","정책"));
		v.add(new Word("position","직책"));
		v.add(new Word("department","부서"));
		v.add(new Word("client","고객"));
		v.add(new Word("supplier","공급업체"));
		v.add(new Word("rate","요금"));
		v.add(new Word("warehouse","창고"));
		v.add(new Word("route","길,노선"));
		v.add(new Word("conservative","보수적인"));
		v.add(new Word("so as to","~하기 위해서"));
		v.add(new Word("unanimously","만장일치로"));
		v.add(new Word("proceeds","수익금"));
		v.add(new Word("be liable for","~에 대한 책임이 있다"));
		v.add(new Word("issue","발표하다"));
	}
	
	public static void main(String[] args) {
		
		int choice= 0;
		String answer ="";
		savedWord();
		Scanner scan = new Scanner(System.in);
		
		while(true) {
			
			int random_q = (int)(Math.random() * v.size());
			if(choice != -1) {
				
				System.out.println("\"영단어 테스트\" 시작합니다. (-1을 입력하면 종료합니다.) ");
				System.out.println("현재" + v.size() +"의 단어가 들어 있습니다.");
				String question = v.get(random_q).englishWord;
				String[] fakeAnswer = new String[4];
				int random = (int)(Math.random() * 4);
				for(int i=0;i<4;i++) {
					if(i == random) {
							answer	= v.get(random_q).koreaWord;
							fakeAnswer[i] = answer;
					}else {
						fakeAnswer[i] = v.get((int)(Math.random() * v.size())).koreaWord;
						if(fakeAnswer[i].equals(answer)) {
							i--;
						}
					}
					
				}
				System.out.println(question + "?");
				System.out.println("(1)" + fakeAnswer[0] + "(2)" + fakeAnswer[1] + "(3)" + fakeAnswer[2] + "(4)" + fakeAnswer[3]);
				choice = scan.nextInt();
				switch(choice) {
				case 1:
					if(answer.equals(fakeAnswer[0])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + answer);
					}
					break;
				case 2:
					if(answer.equals(fakeAnswer[1])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + answer);
					}
					break;
				case 3:
					if(answer.equals(fakeAnswer[2])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + answer);
					}
					break;
				case 4:
					if(answer.equals(fakeAnswer[3])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + answer);
					}
					break;
				}
				
			}else {
				System.out.println("\"영단어 테스트\" 를 종료합니다.");
				System.exit(0);
			}
			
			
		}
		
	}

}

그리고 사용자가 직접 단어를 삽입할 수 있도록 기능 추가하겠습니다.

 

 

 

public static boolean checkWord(String englishWord, String koreaWord) {
		for(int i=0;i<v.size();i++) {
			if(englishWord.equals(v.get(i).englishWord)) {
				System.out.println(englishWord + "은 이미 있는 단어입니다.");
				return false;
			}
		}
		return true;
		
	}


public static void saveWord() {
		System.out.println("영어 단어에 그만을 입력하면 입력을 종료합니다.");
		String input="";
		String error="";
		error = scan.nextLine();
		while(!input.equals("그만")) {
			System.out.println("영어 한글 입력(friend 깃발) >> ");
			input = scan.nextLine();
			System.out.println(v.size());
			if(!input.equals("그만")) {
				String[] array = input.split(" ");
				if(checkWord(array[0],array[1])) {
					v.add(new Word(array[0],array[1]));
				}
			}else {
				break;
			}
		}
	}

 

그리고, 사용자가 틀린 단어는 따로 저장되서 오답노트를 확인할 수 있는 기능을 추가해보겠습니다.

 

public static void reviewWord(String koreaWord, String englishWord) {
		rv.add(new Word(englishWord,koreaWord));
	}
	
	public static void reviewing() {
		if(rv.size() == 0) {
			System.out.println("오답이 없습니다.");
		}else {
			for(int i = 0;i<rv.size();i++) {
				System.out.println(rv.get(i).englishWord + " : " + rv.get(i).koreaWord);
				
			}
		}
		
	}

 

 

최종 코드입니다~ 실력이 늘고 있는 것 같아서 다행입니다!!

 

 

package ex;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;

class Word {
	String englishWord;
	String koreaWord;
	public Word(String englishWord, String koreaWord) {
		this.englishWord = englishWord;
		this.koreaWord = koreaWord;
	}
}
public class EnglishTest {

	static Vector<Word> v = new Vector<Word>();
	static Scanner scan = new Scanner(System.in);
	static Vector<Word> rv = new Vector<Word>();
	
	public static boolean checkWord(String englishWord, String koreaWord) {
		for(int i=0;i<v.size();i++) {
			if(englishWord.equals(v.get(i).englishWord)) {
				System.out.println(englishWord + "은 이미 있는 단어입니다.");
				return false;
			}
		}
		return true;
		
	}
	public static void savedWord() {
		// 사용자가 사용하기전 저장된 단어들
		v.add(new Word("love","사랑"));
		v.add(new Word("notice","공지,주의"));
		v.add(new Word("warranty","보증(서)"));
		v.add(new Word("profit","이익"));
		v.add(new Word("opening","공석"));
		v.add(new Word("estimate","견젹"));
		v.add(new Word("branch","지사"));
		v.add(new Word("policy","정책"));
		v.add(new Word("position","직책"));
		v.add(new Word("department","부서"));
		v.add(new Word("client","고객"));
		v.add(new Word("supplier","공급업체"));
		v.add(new Word("rate","요금"));
		v.add(new Word("warehouse","창고"));
		v.add(new Word("route","길,노선"));
		v.add(new Word("conservative","보수적인"));
		v.add(new Word("so as to","~하기 위해서"));
		v.add(new Word("unanimously","만장일치로"));
		v.add(new Word("proceeds","수익금"));
		v.add(new Word("be liable for","~에 대한 책임이 있다"));
		v.add(new Word("issue","발표하다"));
	}
	
	public static void reviewWord(String koreaWord, String englishWord) {
		rv.add(new Word(englishWord,koreaWord));
	}
	
	public static void reviewing() {
		if(rv.size() == 0) {
			System.out.println("오답이 없습니다.");
		}else {
			for(int i = 0;i<rv.size();i++) {
				System.out.println(rv.get(i).englishWord + " : " + rv.get(i).koreaWord);
				
			}
		}
		
	}
	
	public static void test() {
		int choice= 0;
		while(true) {
			String answer ="";
			int random_q = (int)(Math.random() * v.size());
			if(choice != -1) {
				System.out.println("\"영단어 테스트\" 시작합니다. (-1을 입력하면 종료합니다.) ");
				System.out.println("현재" + v.size() +"의 단어가 들어 있습니다.");
				String question = v.get(random_q).englishWord;
				String[] fakeAnswer = new String[4];
				int random = (int)(Math.random() * 4);
				for(int i=0;i<4;i++) {
					if(i == random) {
							answer	= v.get(random_q).koreaWord;
							fakeAnswer[i] = answer;
					}else {
						fakeAnswer[i] = v.get((int)(Math.random() * v.size())).koreaWord;
						if(fakeAnswer[i].equals(answer)) {
							i--;
						}
					}
					
				}
				System.out.println(question + "?");
				System.out.println("(1)" + fakeAnswer[0] + "(2)" + fakeAnswer[1] + "(3)" + fakeAnswer[2] + "(4)" + fakeAnswer[3]);
				choice = scan.nextInt();
				switch(choice) {
				case 1:
					if(answer.equals(fakeAnswer[0])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + answer);
						reviewWord(answer, question);
					}
					break;
				case 2:
					if(answer.equals(fakeAnswer[1])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + answer);
						reviewWord(answer, question);
					}
					break;
				case 3:
					if(answer.equals(fakeAnswer[2])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + answer);
						reviewWord(answer, question);
					}
					break;
				case 4:
					if(answer.equals(fakeAnswer[3])) {
						System.out.println("Good!");
					}else {
						System.out.println("No..");
						System.out.println("answer is " + answer);
						reviewWord(answer, question);
					}
					break;
				}
				
			}else {
				System.out.println("\"영단어 테스트\" 를 종료합니다.");
				break;
			}
			
			
		}
	}
	
	public static void saveWord() {
		System.out.println("영어 단어에 그만을 입력하면 입력을 종료합니다.");
		String input="";
		String error="";
		error = scan.nextLine();
		while(!input.equals("그만")) {
			System.out.println("영어 한글 입력(friend 깃발) >> ");
			input = scan.nextLine();
			System.out.println(v.size());
			if(!input.equals("그만")) {
				String[] array = input.split(" ");
				if(checkWord(array[0],array[1])) {
					v.add(new Word(array[0],array[1]));
				}
			}else {
				break;
			}
		}
	}
	public static void main(String[] args) {
		
		System.out.println("\"영단어 프로그램\" 시작합니다.");
		savedWord();
		int option = 0;
		while(true) {
			System.out.println("단어 테스트 : 1  단어 삽입 : 2  오답노트 : 3 종료 : 4 ");
			option = scan.nextInt();
			
			switch(option) {
			case 1:
				test();
				break;
			case 2:
				saveWord();
				break;
			case 3:
				reviewing();
				break;
			case 4:
				System.out.println("종료합니다.");
				System.exit(0);
				break;
				default:
					System.out.println("잘못 입력하셨습니다.");
			}
		}
		
	}

}

 

 

 

영어 단어 테스트 프로그램을 만들면서, 컬렉션 각각의 차이를 약간~ 알았습니다.

구분 종류 중복허용 순서존재 정렬여부
List Vector Yes Yes No
Map HashMap No No Yes(Key)

물론, 더 많이 만들어봐야죠~

그럼 다들 열공하세요~

728x90