0

当学生答对问题时,程序会提出另一个随机问题。但是当学生答错问题时,它仍然会问他们一个新问题。我想改变它,这样当学生做错问题时,它会再次问他们同样的问题,直到他们做对为止。请帮忙,非常感谢!

import java.util.Random;
import java.util.Scanner;


public class Multiplication {

    static Random random = new Random();

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int correctCtr =0;
        int wrongCtr = 0;

        System.out.println("Welcome to the CAI-Knowledge Assessment\n ");

        int type = getProblemType(scanner);
        System.out.print("Enter difficulty level: ");
        int difficulty = scanner.nextInt();

        while (true) {
            int questionAns = getQuestion(type, difficulty);
            int answer = scanner.nextInt();

            if (questionAns != answer) {
                System.out.println(getWrongResponse());
                wrongCtr++;


            } else {
                System.out.println(getCorrectResponse());
                correctCtr++;
            }

            if ((wrongCtr + correctCtr) == 10) {
                double correctness = (correctCtr / 10.0) * 100;
                if (correctness > 75) {
                    System.out.println("Congratulations, you are ready to go to the next level!");
                } else {
                    System.out.println("Please ask your teacher for extra help.");
                }
                wrongCtr = 0;
                correctCtr = 0;
                while (true) {
                    System.out.print("Try again? Y or any other character to exit:");
                    String response = scanner.next();
                    if (response.equalsIgnoreCase("y")) {
                        type = getProblemType(scanner);
                        System.out.print("Enter difficulty level: ");
                        difficulty = scanner.nextInt();
                        break;
                    } else {
                        System.out.println("Thank you for learning!");
                        System.exit(0);
                    }
                }
            }

        }
    }

    static int getProblemType(Scanner scanner) {
        int type;
        while (true) {

            System.out.print("What type of problem would you like? \n(1=addition, 2=subtraction, 3=multiplication, 4=division, 5=mixture): ");
            type = scanner.nextInt();
            if (type > 0 && type < 6) {
                break;
            } else {
                System.out.println("Invalid type!");
            }
        }
        return type;
    }

    static String getCorrectResponse() {
        String response = null;
        int index = random.nextInt(4);
        switch (index) {
        case 0:
            response = "Very good!";
            break;
        case 1:
            response = "Excellent!";
            break;
        case 2:
            response = "Nice work!";
            break;
        case 3:
            response = "Keep up the good work!";
            break;
        }
        return response;
    }

    static String getWrongResponse() {
        String response = null;
        int index = random.nextInt(4);
        switch (index) {
        case 0:
            response = "No. Please try again.";
            break;
        case 1:
            response = "Wrong. Try once more.";
            break;
        case 2:
            response = "Don't give up!";
            break;
        case 3:
            response = "No. Keep trying.";
            break;
        }

        return response;
    }

    static int getQuestion(int type, int difficulty) {
        int no1 = random.nextInt((int)Math.pow(10, difficulty));
        int no2 = random.nextInt((int)Math.pow(10, difficulty));

        int returnVal = 0;
        if (type == 1) {
            System.out.printf("How much is %d plus %d? ",no1,no2);
            returnVal = no1 + no2;
        } else if (type == 2) {
            while (no2 > no1) {
                no2 = random.nextInt((int)Math.pow(10, difficulty));
            }
            System.out.printf("How much is %d minus %d? ",no1,no2);
            returnVal = no1 - no2;
        } else if (type == 3) {
            System.out.printf("How much is %d times %d? ",no1,no2);
            returnVal = no1 * no2;
        } else if (type == 4) {
            while (no2 == 0 || no2 > no1 || no1%no2!=0) {
                no2 = random.nextInt((int)Math.pow(10, difficulty));
            }
            System.out.printf("How much is %d divided by %d? ",no1,no2);
            returnVal = no1 * no2;
        } else if (type == 5) {
            returnVal = getQuestion(random.nextInt(5)+1, difficulty);
        }

        return returnVal;
    }
}
4

6 回答 6

1

您可以通过多种方式进行操作。一个简单的 hack 可以在如图所示的同一点进行迭代-

int answer = scanner.nextInt();

if (questionAns != answer) {
          *while (questionAns != answer)*{
            wrongAnswer();
            System.out.println("Try Again");
            int answer = scanner.nextInt();
          }
        }

static void wrongAnswer(){
     System.out.println(getWrongResponse());
     wrongCtr++;
}   

PS我没有测试过代码。祝你好运。

于 2013-09-28T02:48:58.403 回答
1

你自己说的

Set<String> questions = getAllQuestions();
for( String question : questions) {
   boolean correctAnswer = false;
   while(!correctAnswer) {
      correctAnswer = askQuestion(question);
   }
}

现在,只需填写空白。实现模板算法中的方法。如果该人不知道正确答案,为避免陷入无限循环,您可能希望在尝试一定次数后打破循环。我相信您现在可以很容易地将该逻辑添加到模板算法中。

于 2013-09-28T02:38:54.210 回答
0

只需设置另一个变量来跟踪他们的问题是否正确,然后仅在该变量为假时才重置问题。

Boolean questionCorrect = true;
while (true) {
        if (questionCorrect) {
            int questionAns = getQuestion(type, difficulty);   
        }
        int answer = scanner.nextInt();

        if (questionAns != answer) {
            System.out.println(getWrongResponse());
            wrongCtr++;
            questionCorrect = false;

        } else {
            System.out.println(getCorrectResponse());
            correctCtr++;
            questionCorrect = true;
        }
于 2013-09-28T02:33:37.990 回答
0

当您随机生成no1, no2. 所以,你应该把你的问题放在全局变量中。getQuestion(int type, int difficulty)在方法中分配变量。这是示例代码。

String question=""

   static int getQuestion(int type, int difficulty){
     if(type==2){
       question=String.format("How much is %d minus %d? ",no1,no2);
      }
   }

然后你可以问同样的问题,如果用户弄错了。

于 2013-09-28T02:34:11.487 回答
0

您应该简单地缓存一个 questionAns 实例(您的 int)和一个字符串变量,该变量在初始化时具有与您要打印到输出的格式相同的格式(您可以通过 String.format() 执行此操作)。

完成此操作后,您还可以包含一个临时布尔值,该布尔值根据用户提供的正确或错误值进行更改。然后将在 while 循环的下一次迭代中读取此布尔值,然后让您选择将新问题打印到屏幕(通过您的方法)或打印您保存的格式化字符串,这是上一个问题。

于 2013-09-28T02:35:30.687 回答
0

添加 2 个新变量

boolean isTrue = true;
boolean isFalse = true;

并编辑此部分

if (isTrue)    
    int questionAns = getQuestion(type, difficulty);
int answer = scanner.nextInt();

if (questionAns != answer) {
    System.out.println(getWrongResponse());
    wrongCtr++;
    isTrue = false;
    isFalse = true;
} else {
    System.out.println(getCorrectResponse());
    correctCtr++;
    isTrue = true;
    isFalse = false;
}
于 2013-09-28T02:35:33.603 回答