Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have compile and run this program and it works just fine, but i wanna add 1 more function into it which is when the total answered question is more than 75% the system will print "Congratulation, you are ready for the next level", or else it will print "You have to see your teacher". So my question is who can help me do the calculation of the correct answer because i dont know how to calculate the percentage of the correct answer :(

What I have tried:

Java
import java.util.*;

class learnMultiplication
{
   public static void main(String args[])
   {
		int correctAnswers = 0;
		int actualAnswer, userAnswer;
		int randomNum[] = new int[2];
		String question;
		Scanner sc = new Scanner(System.in);
      
       //Loop till user answers 10 correct questions
       while(correctAnswers <= 9)
       {
           //Generate a question
           question = newQuestion(randomNum);

           //Calculate actual answer
           actualAnswer = randomNum[0] * randomNum[1];

           //Display question to user
           System.out.print("\n " + question+"\n");
          
           //Get answer from user
           userAnswer = sc.nextInt();
		   
		   

           //If both answers doesn't match loop both of them matches
           while(userAnswer != actualAnswer)
           {
               //Display a wrong message to user
               displayWrongMessage();

               //Displaying the same question again
               System.out.println("\n " + question+"\n");
              
               //Get answer from user
               userAnswer = sc.nextInt();
           }

           //Once userCorrectMe answers correctly display correct message
           displayCorrectMessage();

           //Counting total number of correct answers
           correctAnswers++;
		   

       }
	   
	   
	   
   }
  
   //Method that displays a new question to user
   public static String newQuestion(int randomNum[])
   {
       String question;
      
       int Min = 1, Max = 4;

       //Generate two 1 digit integers randomly
       randomNum[0] = Min + (int)(Math.random() * ((Max - Min) + 1));
       randomNum[1] = Min + (int)(Math.random() * ((Max - Min) + 1));
      
       //Forming question
       question = " How much is " + randomNum[0] + " times " + randomNum[1] + " ? ";

       //Return question to user
       return question;
   }
  
   //Method that displays wrong message to user
   public static void displayWrongMessage()
   {
       int index;
       String message = "";

       //Generate a random number for wrong answer 
       int Min = 0, Max = 3;
       index = Min + (int)(Math.random() * ((Max - Min) + 1));
      
       //Selecting message
       switch(index)
       {
           case 0: message = "No. Please try again."; 
					break;
           case 1: message = "Wrong. Try once more."; 
					break;
           case 2: message = "No. Don't give up!"; 
					break;
           case 3: message = "No. Keep trying."; 
					break;
           default: break;
       }

       //Printing the message present at the generated index
       System.out.println("\n " + message + " \n");
   }
  
   //Method that displays correct message to user
   public static void displayCorrectMessage()
   {
       int index;
       String message = "";

       //Generate a random number for correct answer 
       int Min = 0, Max = 3;
       index = Min + (int)(Math.random() * ((Max - Min) + 1));
      
       //Selecting message
       switch(index)
       {
           case 0: message = "Very good!"; 
					break;
           case 1: message = "Excellent!"; 
					break;
           case 2: message = "Nice work!"; 
					break;
           case 3: message = "Keep up the good work!"; 
					break;
           default: break;
       }

       //Printing the message present at the generated index
       System.out.println("\n " + message + " \n");
   }
   
 
}
Posted
Updated 8-Oct-16 18:17pm
v3
Comments
[no name] 8-Oct-16 14:08pm    
"but i wanna add 1 more function", okay then go ahead and do that. You have permission to add your function. Not sure why you have to have our permission to do this....
Suvendu Shekhar Giri 8-Oct-16 14:34pm    
So what is the issue here?
Patrice T 8-Oct-16 17:23pm    
What is the problem with the calculation of percentage of good answers ?
Member 12783195 8-Oct-16 23:16pm    
yea i dont know how to calculate it
Patrice T 8-Oct-16 23:31pm    
Are you sure ?
This is elementary school !

1 solution

Quote:
i dont know how to calculate the percentage of the correct answer
You need to learn percentages and fractions. Both are elementary school mathemarics.
The bad news is that while programming, you will have to deal continuously with such things or similar ones.
https://en.wikipedia.org/wiki/Percentage[^]
https://en.wikipedia.org/wiki/Fraction_%28mathematics%29[^]

[Update]
Quote:
i dont know how to add function to count the total number of correct answer from the answered question.
I fear you already have it with variable correctAnswers.
Advice: you should study the code you found on internet before asking for help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900