プログラミングのクラスでこのプロジェクトを実行する必要があります。このプロジェクトでは、ユーザーが解決したい数学の問題の数、希望する難易度 (簡単または難しい) を入力し、入力に基づいて数学の問題をランダムに尋ねる必要があります。なんらかの理由で、難易度を尋ねてからコードを操作することができません。問題は、オペレーターをランダムに選択しようとしている方法だと思いますが、よくわかりません。これが私のコードです:
import java.util.Scanner;
import java.util.Random;
public class MathPractice {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int answer;
int count = 0;
int solution;
int correct = 0;
int numEasy1 = rand.nextInt(10);
int numEasy2 = rand.nextInt(10);
int numHard1 = rand.nextInt(20) - 9;
int numHard2 = rand.nextInt(20) - 9;
int sign = rand.nextInt(4);
System.out.print("How many problems do you want? ");
int number = keyboard.nextInt();
System.out.print("What level difficulty would you prefer (easy/hard)? ");
String difficulty = keyboard.next();
if (difficulty == "easy" || difficulty == "Easy") {
while (count < number) {
if (sign == '0') {
System.out.print(numEasy1+" + "+numEasy2+" = ");
answer = keyboard.nextInt();
solution = numEasy1 + numEasy2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '1') {
System.out.print(numEasy1+" - "+numEasy2+" = ");
answer = keyboard.nextInt();
solution = numEasy1 - numEasy2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '2') {
System.out.print(numEasy1+" * "+numEasy2+" = ");
answer = keyboard.nextInt();
solution = numEasy1 * numEasy2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign =='3') {
System.out.print(numEasy1+" / "+numEasy2+" = ");
answer = keyboard.nextInt();
solution = numEasy1 / numEasy2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
}//inner if statement
}//while loop
} else if (difficulty == "hard" || difficulty == "Hard") {
while (count < number) {
if (sign == '0') {
System.out.print(numHard1+" + "+numHard2+" = ");
answer = keyboard.nextInt();
solution = numHard1 + numHard2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '1') {
System.out.print(numHard1+" - "+numHard2+" = ");
answer = keyboard.nextInt();
solution = numHard1 - numHard2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '2') {
System.out.print(numHard1+" * "+numHard2+" = ");
answer = keyboard.nextInt();
solution = numHard1 * numHard2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '3') {
System.out.print(numHard1+" / "+numHard2+" = ");
answer = keyboard.nextInt();
solution = numHard1 / numHard2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
}//inner if statement
}//while loop
} //if statement
System.out.println("You got "+correct+" correct out of "+number+".");
}
}