import java.util.Scanner; import java.util.Random; public class Ex08_4 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); Random rnd = new Random(); String msg0 = "This is a pen."; String msg1 = "What time is it now?"; String msg2 = "Shall we dance?"; String que, ans; char flg = 'y'; int i, sel; System.out.println("-------------"); System.out.println(" Typing Game "); System.out.println("-------------"); while ( flg == 'y' ) { // flg == 'y' である限り繰り返す sel = rnd.nextInt(3); // 問題文をランダムに選択 switch ( sel ) { case 0: que = msg0; break; // 問題文0 case 1: que = msg1; break; // 問題文1 case 2: que = msg2; break; // 問題文2 default: System.out.println("error: 予期せぬエラー"); return; } System.out.println("Q: " + que); System.out.print("A: "); ans = stdin.nextLine(); // ユーザーのタイピング(入力)を読み込み // 正解判定1:長さ判定 if ( que.length() != ans.length() ) { // 問題文 que とユーザー入力 ans の長さが違えば不正解 System.out.println(" ぶっぶー。間違いだよ〜\n\n--- Game over --- "); return; } // 正解判定2:文字列照合 for (i=0; i "); flg = stdin.nextLine().charAt(0); } System.out.println("終了します。さよなら、また来てね。。。"); } }