java - 新类(class),无法通过它们验证数学问题

标签 java

我正在尝试为作业构建一个简单的数学程序。我花了一个多小时,但我似乎无法正确验证方程。其他一切似乎都有效。问题是,当需要验证问题数量是否正确时,它只计算最后一个。 所以如果我答对了 3/3,它就会说 3 分之1 是对的。如果我前两个正确,第二个错误,它会说 3 中的 0 是正确的。

这是我的代码:

import java.util.Scanner;
public class ElementaryMath {

    private double num1;
    private double num2;
    private String opType;
    private int numCorrect;
    private int actualAns;

    ElementaryMath(double n1, double n2, String ot){
        num1=n1;
        num2=n2;
        opType=ot;
    }

    public void printQuestion(){
        System.out.println((int)num1 + " " + opType + " " + (int)num2);
    }

    public void checkAnswer(int ans, String op){

        if(op.equals("/")) {
            actualAns = (int) num1 / (int) num2;
            System.out.println("answer is " + actualAns +" "+ ans);
        }
        if(op.equals("*")){
            actualAns = (int) num1 * (int) num2;
            System.out.println("answer is " + actualAns + " " + ans);
        }

        if(actualAns == ans){
            numCorrect++;
        }

    }

    public String numCorr(){
        return ""+numCorrect+"";
    }

    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        double n1, n2;
        String op = "";
        int nq, qt, aq;

        System.out.println("Which option would you like?");
        System.out.println("1. Single Digit - One operation");
        System.out.println("2. Two Digit -  One operation.");
        System.out.println("3. Single Digit - Multiple Operations (Mixed operators)");
        System.out.println("");
        System.out.println("Please enter your option. (1, 2, or 3): ");
        qt = in.nextInt();
        if(qt==1 || qt==2) {
            System.out.println("Please enter which operator you would like to use: (type / or * )");
            op = in.next();
        }
        System.out.println("Enter the number of questions you would like: ");
        nq = in.nextInt();

        if(qt==1 || qt==2)
        {
        for(int i=0;i<nq;i++) {
            n1 = Math.random() * 9 + 1;
            n2 = Math.random() * 9 + 1;
            ElementaryMath a = new ElementaryMath(n1, n2, op);
            a.printQuestion();
            System.out.println("Please print your answer: ");
            aq = in.nextInt();
            a.checkAnswer(aq, op);
            if(i==(nq-1)) {
                System.out.println("You have successfully answered " + a.numCorr() + " out of " + nq + " questions correctly.");
                }
            }
        } 
    }
}

最佳答案

您的问题是您需要一个静态计数器变量(numCorrect)。然后,您需要检查给出的答案是否正确。这是修改后的代码。

import java.util.Scanner;
public class ElementaryMath {

    private double num1;
    private double num2;
    private String opType;
    private static int numCorrect;
    private int actualAns;

    ElementaryMath(double n1, double n2, String ot){
        num1=n1;
        num2=n2;
        opType=ot;
    }

    public void printQuestion(){
        System.out.println((int)num1 + " " + opType + " " + (int)num2);
    }

    public void checkAnswer(int ans, String op){

        if(op.equals("/")) {
            actualAns = (int) num1 / (int) num2;
            System.out.println("answer is " + actualAns +" "+ ans);
            if(actualAns == ans)
            {
                numCorrect++;
            }
        }
        if(op.equals("*")){
            actualAns = (int) num1 * (int) num2;
            System.out.println("answer is " + actualAns + " " + ans);
            if(actualAns == ans)
            {
                numCorrect++;
            }
        }
    }

    public String numCorr(){
        return ""+numCorrect+"";
    }

    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        double n1, n2;
        String op = "";
        int nq, qt, aq;

        System.out.println("Which option would you like?");
        System.out.println("1. Single Digit - One operation");
        System.out.println("2. Two Digit -  One operation.");
        System.out.println("3. Single Digit - Multiple Operations (Mixed operators)");
        System.out.println("");
        System.out.println("Please enter your option. (1, 2, or 3): ");
        qt = in.nextInt();
        if(qt==1 || qt==2) {
            System.out.println("Please enter which operator you would like to use: (type / or * )");
            op = in.next();
        }
        System.out.println("Enter the number of questions you would like: ");
        nq = in.nextInt();

        if(qt==1 || qt==2)
        {
            for(int i=0;i<nq;i++) {
                n1 = Math.random() * 9 + 1;
                n2 = Math.random() * 9 + 1;
                ElementaryMath a = new ElementaryMath(n1, n2, op);
                a.printQuestion();
                System.out.println("Please print your answer: ");
                aq = in.nextInt();
                a.checkAnswer(aq, op);
                if(i==(nq-1)) {
                    System.out.println("You have successfully answered " + numCorrect + " out of " + nq + " questions correctly.");
                }
            }
        }
    }
}

这可以解决您的问题,例如计数错误。

关于java - 新类(class),无法通过它们验证数学问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814388/

相关文章:

java.lang.ClassNotFoundException : org. apache.struts.taglib.bean.CookieTei

java - Repainting Frame 删除之前绘制的形状

java - JDK动态代理的优点

Java 多线程与 CompletableFuture 运行速度较慢

java - cucumber testng 运行程序失败

java - Micrometer (micrometerClock) 中的错误处理条件

java - 确定来自另一个 Java 应用程序的 Java 执行是否成功完成

java - 如何从源码制作midlet?

java - 我在类(class)的什么地方放置@XmlElement 注释?

java - charAt() 或子字符串?哪个更快?