1

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

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+".");
}

}

4

3 回答 3

0

您需要更改if (difficulty == "easy" || difficulty == "Easy")if (difficulty.equalsIgnoreCase("easy"))“硬”循环并对其执行相同的操作。

此外,您已sign声明为 anint但您将其比较为char. 将所有(sign == '3')行更改为(sign == 3),您应该会很好。

于 2013-10-22T05:34:00.060 回答
0

尝试使用nextLine. 这将在输入后消耗换行符

    System.out.print("How many problems do you want? ");
    int number = keyboard.nextInt();
    keyboard.nextLine();
    System.out.print("What level difficulty would you prefer (easy/hard)? ");
    String difficulty = keyboard.next();

并使用 equals 方法比较字符串值,==将比较对象引用。

于 2013-10-22T05:16:30.613 回答
0

您的代码if (sign == '0') {将 rand.nextInt() 函数的整数形式与字符进行比较。更改'0'为仅0适用于所有 if 语句

于 2013-10-22T05:18:45.733 回答