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として宣言しましたintが、それを であるかのように比較します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() 関数の整数を char と比較しています。すべての if ステートメントのみに変更'0'します0

于 2013-10-22T05:18:45.733 に答える