import java.util.Scanner;
class Dictionary{
private static String[] kor = {"사랑","아기","돈","미래","희망"};
private static String[] eng = {"love","baby","money","future","hope"};
public static String kor2Eng(String word) {
for(int i =0;i<kor.length;i++) {
//사전 안에 있으면 해석본으로 보낸다
if(word.equals(kor[i])) {
String translate = eng[i];
return translate;
}
}
//사전 안에 없으면 검색어 그대로 보낸다.
return word;
}
}
public class DicApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("한영 단어 검색 프로그램입니다.");
while(true) {
System.out.print("한글 단어?");
String word = sc.next();
if(word.equals("그만")) {
break;
}
//검색어랑 kor2Eng이 같으면 사전에 없는 단어
if(word.equals(Dictionary.kor2Eng(word))) {
System.out.println(word+"는 저의 사전에 없습니다.");
}
//그 밖에 있는 것들은 사전에 있는 단어
else {
System.out.println(word+"은 "+Dictionary.kor2Eng(word));
}
}
}
}
결과
한영 단어 검색 프로그램입니다.
한글 단어?희망
희망은 hope
한글 단어?아가
아가는 저의 사전에 없습니다.
한글 단어?아기
아기은 baby
한글 단어?그만
'명품JAVA프로그래밍 > 4장 클래스와 객체' 카테고리의 다른 글
[명품JAVA프로그래밍] 4장 실습문제 12번 (0) | 2021.12.27 |
---|---|
[명품JAVA프로그래밍] 4장 실습문제 11번 (0) | 2021.12.27 |
[명품JAVA프로그래밍] 4장 실습문제 9번 (0) | 2021.12.27 |
[명품JAVA프로그래밍] 4장 실습문제 8번 (0) | 2021.12.27 |
[명품JAVA프로그래밍] 4장 실습문제 7번 (0) | 2021.12.27 |