繁体   English   中英

如何检查用户答案与测验中的代数答案相同?

[英]How Do I Check If User Answer Is Same As Algebra Answer In Quiz?

我正在用Java进行代数测验,我想检查用户的答案是否与代数方程式的答案相同。 这就是我已经得到的,检查已接近底部。 第一个if语句正在检查第二个数字是正数还是负数并进行计算。 第二个if语句试图查看用户答案是否与问题答案相同。 问题是Int答案不能转移到另一个if语句。 有办法解决这个问题吗? 它使用的是这样的代数方程:1x + 2 = 5。

        int numRight = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to your algebra test!");
    System.out.println("What is your name?");
    String name = input.nextLine();
    System.out.println("Ok, " + name + ", how many questions do you want on your test?");
    int numQuestions = input.nextInt();

    for(int x = 0; x <= numQuestions; x++){

        int coefficient = 7;
        int num1 = 3;
        int num2 = 23;

        while((num2 - num1) % coefficient != 0){

        coefficient = (int) (Math.random()*8)+2;
        num1 = (int) (Math.random()*19)-9;
        num2 = (int) (Math.random()*90)+10;

        }


                        if(num1 > 0){
                int part1pos = num2 - num1;
                int answer = (coefficient / part1pos);
            } else if(num1 < 0){
                int part1neg = num1 + num2;
                int answer = (coefficient / part1neg);
            }

        System.out.println(coefficient + "x + " + num1 + " = " + num2);
        int userAnswer = input.nextInt();

        if(userAnswer == answer){
            System.out.println("The Answer is correct!");
            numRight++;
        } else{
            System.out.println("The answer is wrong!");
        }


    }

问题是您的答案整数在与检查答案正确性的布尔语句不同的范围内声明。 您应该在该布尔型语句之外(在您声明系数 num1num2的位置附近)声明并初始化为0的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM