Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created January 9, 2017 12:25
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/5db9ccf82268dcc791760d363fe0b25a to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/5db9ccf82268dcc791760d363fe0b25a to your computer and use it in GitHub Desktop.
新・明解Java入門 演習7-10
import java.util.Random;
import java.util.Scanner;
public class En07_10 {
static Scanner stdIn = new Scanner(System.in);
public static void main(String[] args) {
Random rand = new Random();
System.out.println("暗算力トレーニング!!");
do {
int randomTest = rand.nextInt(4);
int x = rand.nextInt(900) + 100; // 3桁の数
int y = rand.nextInt(900) + 100; // 3桁の数
int z = rand.nextInt(900) + 100; // 3桁の数
Roop: while (true) {
switch (randomTest) {
case 0:
System.out.print(x + " + " + y + " + " + z + " = ");
break;
case 1:
System.out.print(x + " + " + y + " - " + z + " = ");
break;
case 2:
System.out.print(x + " - " + y + " + " + z + " = ");
break;
case 3:
System.out.print(x + " - " + y + " - " + z + " = ");
break;
}
int k = stdIn.nextInt(); // 読み込んだ値
switch (randomTest) {
case 0:
if (k == x + y + z) {
break Roop;
} else {
break;
}
case 1:
if (k == x + y - z) {
break Roop;
} else {
break;
}
case 2:
if (k == x - y + z) {
break Roop;
} else {
break;
}
case 3:
if (k == x - y - z) {
break Roop;
} else {
break;
}
}
System.out.println("違いますよ!");
}
} while (confirmRetry());
}
// 続行の確認
static boolean confirmRetry() {
int cont;
do {
System.out.print("もう一度?<Yes…1/No…0>:");
cont = stdIn.nextInt();
} while (cont != 0 && cont != 1);
return cont == 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment