我必须为我的编程课做这个项目,它要求我们提示用户他们想要解决多少数学问题,他们想要的难度(简单或困难),然后根据他们的输入随机询问数学问题。出于某种原因,我无法让代码在询问难度级别之后运行。我认为问题在于我尝试随机选择运算符的方式,但我不确定。这是我的代码:
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+".");
}
}