Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created December 14, 2016 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokomokohitsuzi/761d3012e95f864f58e01b1d2c93999c to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/761d3012e95f864f58e01b1d2c93999c to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-14
import java.util.Random;
import java.util.Scanner;
public class En06_14 {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
// 月を作成
String[] Month = { "January", "February", "March", "April", "May", "Jun", "July", "August", "September",
"October", "November", "December" };
// 問題を表示。
System.out.println("英語の月名を入力して下さい。");
System.out.println("なお、先頭は大文字で、2文字目以降は小文字とします。");
// 1つ前の問題が出ないようにする。(最初の問題の過去問は0~11以外の数字を入れておく)
int ransu;
int exam0 = -1;
Roop: while (true) {
do {
// 問題用乱数の作成
ransu = rand.nextInt(12);
} while (ransu == exam0);
// 過去問に乱数を代入。
exam0 = ransu;
// ループ用ラベル1
Retry: while (true) {
// 配列は0から始まるため、実際の月を表示する場合は+1する。
System.out.printf("%d月:", ransu + 1);
// 文字列を入力させる。
String answerMonth = stdIn.next();
// 文字列の比較を実施
if (Month[ransu].equals(answerMonth)) {
System.out.print("正解です。もう一度? 1…Yes/0…No:");
int roop = stdIn.nextInt();
if (roop == 0) {
// 終了する場合は、Roopを抜ける
break Roop;
} else {
// 最初に戻る
continue Roop;
}
} else {
System.out.println("違います");
// Retryラベルまで戻る
continue Retry;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment