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.