Kylian Murphy Kylian Murphy - 8 days ago 5
Java Question

New to classes, Unable to verify math problems through them

I'm trying to build a simple math program for an assignment. I've spent over an hour on it but I can't seem to get the equations to verify properly. Everything else seems to work. The problem is that when it comes time to verify the # of questions right, it only ever counts the last one.
So if I got 3/3 right, it will say 1 out of 3 is right. If I get the first two right and the second wrong, it will say 0 out of 3 is right.

Here is my code:

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

Answer

What your problem was was that you needed a static counter variable(numCorrect). Then you needed to check if the answer given and the answer is correct. Here is the modified code.

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

This fixes your issues such as miscounting, accept this answer if this is what you're looking for :)

Comments