JAVA 쿵쿵따 게임 #2 중복 탐지
package com.tistory.tansanc;
import java.util.HashSet;
import java.util.Scanner;
public class User {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] word = {"기러기", "기차표","스피커"};
String oldWord = word[
(int)(Math.random()*3) ];
HashSet<String> hs = new HashSet<String>();
hs.add(oldWord);
System.out.println("제시어 : " + oldWord);
String newWord = sc.nextLine();
if( ! hs.add(newWord) )
{
System.err.println("중복 발생!");
}
if( oldWord.charAt(2) ==
newWord.charAt(0) )
{
System.out.println("OK");
}
else
{
System.err.println("ERROR");
}
while(true)
{
oldWord = newWord;
newWord = sc.nextLine();
if( ! hs.add(newWord) )
{
System.err.println("중복 발생!");
}
if( oldWord.charAt(2) ==
newWord.charAt(0) )
{
System.out.println("OK");
}
else
{
System.out.println("ERROR");
}
}
}
}