Prithvi Boinpally
Prithvi Boinpally

Reputation: 485

How to calculate percentage base on user input in java

So basically I have this survey program and I want to know how to calculate the percentage of questions answered that match the response given by the "if" statements. but this percentage can vary based on what the user types. another thing is that I need to put the code for calculating percentage into a non-main method which I have already setup and commented on (labeled non-main method) in the code below. What i want to know is how to make a formula that will account for all possible user inputs (basically 1/5, 2/5, 3/5, 4/5 and 5/5 since the survey is 5 questions) .

import java.util.Scanner;

public class VideogameSurveyFINAL {    
 //non-main method 

   private static void gamerpercentagelikeme (String[] args) {

   int correct
   }   




  //main method below
   public static void main(String[] args) {
  boolean run = true;
  while(run){

     System.out.println ("WELCOME TO THE GRAND SURVEY OF VIDEOGAMES!");
     System.out.println ("");

     Scanner keyboard = new Scanner(System.in);

  //Q1
     System.out.println("Question 1: Do you like Videogames? (Please answer Yes or No)"); 
     String input = keyboard.nextLine(); 
     if  (input.equalsIgnoreCase("Yes")||input.equalsIgnoreCase("Yeah")||input.equalsIgnoreCase("Y")||input.equalsIgnoreCase("YES!"))  { 
        System.out.println("Awesome!");
     }
     else { 
        System.out.println("Wow. You have no sense of fun.");
     } 
  //Q2
     System.out.println ("");
     System.out.println("Question 2: What types of videogames do you play?(Please use abbreviated froms of games such as FPS, MMO, RPG etc)."); 
     String input2 = keyboard.nextLine(); 
     if  (input2.equalsIgnoreCase("Fps"))  { 
        System.out.println("Fantastic! Me too!");
     }
     else { 
        System.out.println("OK, thats cool!My favorite is FPS.");
     } 
  //Q3
     System.out.println ("");
     System.out.println("Question 3:Do you like Singleplayer or Multiplayer games? "); 
     String input3 = keyboard.nextLine(); 
     if  (input3.equalsIgnoreCase("Singleplayer")||input.equalsIgnoreCase("S")||input.equalsIgnoreCase("SP"))  { 
        System.out.println("Me Too!!");
     }
     else { 
        System.out.println("Rad! My favorite is Singleplayer. ");
     }
  //Q4
     System.out.println ("");
     System.out.println("Question 4: What do you like in a videogame"); 
     String input4 = keyboard.nextLine(); 
     if  (input4.equalsIgnoreCase("I dont play games")||input.equalsIgnoreCase("None")||input.equalsIgnoreCase("N"))  { 
        System.out.println("You are sooooo boring.");
     }
     else { 
        System.out.println("Awesome! What I like in a videogame is solid gameplay, great graphics and immersive audio...but most of all, IT HAS TO BE FUN!!");
     } 
  //Q5
     System.out.println ("");
     System.out.println("Question 5: What is your ALL-TIME favorite videogame?");
     String input5 = keyboard.nextLine(); 
     if  (input5.equalsIgnoreCase("Skyrim"))  { 
        System.out.println("Really?! ME TOO!");
     }
     else { 
        System.out.println("Great! My personal favorite is SKYRIM!");
     } 
     System.out.println ("");
     System.out.println("THANK YOU FOR TAKING THE GRAND SURVEY OF VIDEOGAMES!");
     System.out.println ("");
     System.out.println ("Would you like to take the survey again? (Please answer with Yes or No: Case sensitive)");
     String input6 = keyboard.nextLine(); 
     if  (input6.equalsIgnoreCase("No"))  { 
        break;
     }
     else 
     { 
        System.out.println("Okay! The Survey will restart shortly.");
        System.out.println ("");
        System.out.println ("");




     }
  }

} }

Upvotes: 0

Views: 8397

Answers (2)

Aaron C
Aaron C

Reputation: 343

Right inside of public static void main(String[] args) { declare a public variable like:

int questionsCorrect = 0; //this is where we will keep track of the questions 'correct'

Now every time we get a question right, we will increment this count. Inside of the if(condition...) statements, we will do this increment.

For example:

if  (input.equalsIgnoreCase("Yes")||input.equalsIgnoreCase("Yeah")||input.equalsIgnoreCase("Y")||input.equalsIgnoreCase("YES!"))  { 
    System.out.println("Awesome!");
    questionsCorrect++; //increment number of questions correct by 1
 }

do this for every question in the survey.

Now for the percentage... I believe that it would be wise to change the private static void gamerpercentagelikeme (String[] args) method's parameters to take the questions correct and the total questions, then returning the percentage in decimal form.

private static double gamerpercentagelikeme (int correct, int total){
     return correct/total;
}

Now at the end when we want to display the answer, we can call:

double results = gamerpercentagelikeme(questionsCorrect, TOTAL_QUESTIONS); //note: declare TOTAL_QUESTIONS before using it.
double percentageLikeMe = results * 100;
System.out.println(percentageLikeME + "% like me.);

Hope this helps.

Upvotes: 1

Adam B
Adam B

Reputation: 3861

If i'm understanding correctly, you need to calculate the percentage of questions that the user got right?

You need to have a new field that is an int that stores the number of questions they got right, initialized to zero. Then every time they get a question right you increment this total by one.

Then at the end of the program you do that field divided by the total number of questions

Upvotes: 0

Related Questions