Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
新・明解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