0

I am having a logic error regarding calculating the percentage in my code:

public class Project4App {
/**
 * <p> Name: main method </p>
 * 
 * @param args values to be sent to the method
 */
public static void main(String [] args)
{
    //instance variables
    Scanner scan = new Scanner(System.in);
    int percentage = 0;
    int questions = 0;
    int correctAddition = 0;
    int wrongAddition = 0;
    int correctSubtraction = 0;
    int wrongSubtraction = 0;
    boolean isOver = false;
    //beginning of the while loop 
    //since we don't know 
    //the exact number of times
    //it will loop
    while( !isOver )
    {
        questions++;
        Question q = new Question(); // creating a question object
        System.out.println("What is the result?");
        System.out.println(q);
        int answer = scan.nextInt(); //getting the user input
        if(answer == q.determineAnswer() && q.getOperator() == '+') //right addition
        {
            System.out.println("Congratulations, you got it correct!");
            correctAddition++;
        }
        else if(answer == q.determineAnswer() && q.getOperator() == '-') //wrong addition
        {
            System.out.println("Congratulations, you got it correct!");
            correctSubtraction++;
        }
        else if(!(answer == q.determineAnswer()) && q.getOperator() == '+') //right subtraction
        {
            System.out.println("The correct answer for " + q + " is " + q.determineAnswer());
            wrongAddition++;
        }

        else //catch all; wrong subtraction
        {
            System.out.println("The correct answer for " + q + " is " + q.determineAnswer());
            wrongSubtraction++;
        }


        if( (questions >= 10 && percentage >= 85.0) || questions == 20) //ends loop on these conditons
        {
            isOver = true;

        }
    }
    percentage = ((correctAddition + correctSubtraction) / questions) * 100; //calculating percent
    double newPercentage = (double)percentage; // converting to a double
    int wholePercentage = (int) newPercentage; // converting to a whole number
    //printing the results 
    System.out.println("Progress Report:");
    System.out.println("Addition:");
    System.out.println("You got " + correctAddition + " correct and " + wrongAddition + " incorrect.");
    System.out.println("Subtraction:");
    System.out.println("You got " + correctSubtraction + " correct and " + wrongSubtraction + " incorrect.");
    System.out.println("The percent correct is " + wholePercentage + ".0%");

}

}

During the output: it keeps displaying, "The Percent correct is 0.0%" I tried to place the percentage inside of the while loop but it gave the same output. Any solution regarding this issue is appreciated. Thanks.

Ryan
  • 11
  • 1

2 Answers2

0

Please try using this

percentage = ((float)(correctAddition + correctSubtraction) / questions) * 100;

0

Your are doing integer division and the below line from you code will always give you "zero": percentage = ((correctAddition + correctSubtraction) / questions) * 100; //value of percentage will always be zero

As a result, the value of newPercentage is also zero. Which leads you to wrong answer. Because percentage variable in your code is an integer variable, in Java dividing integer a by integer b you get how many times b fits into a. For example:

int x = 10/12;
int y = x * 100; // value of x will be always zero, same goes for y

A quick fix could be declare percentage as double and modify you code like below:

double percentage = ((correctAddition * 1.0 + correctSubtraction) / questions) * 100;

when you do "(correctAddition * 1.0)" the whole addition will be operated as double. The answer generated will be a double as well. To make things more clear:

double x = (12 * 1.0)+10; // you will get  a double value, not zero.

Edit: A more allegiant solution could be:

double percentage = (((double)correctAddition + correctSubtraction) / questions) * 100;
kayas
  • 703
  • 1
  • 5
  • 14