简体   繁体   中英

How to setup a method to calculate a percentage?

So I have to set up a Method called CalculatePercentage() and it has two parameters integers called: ( int correctAnswer, int numberOfQuestions ) the method will return to correctAnswer . So I have a basic idea of how to set up the method, for the formula is something like this (correctAnswer/numberOfQuestions)*100 and I also have to set up a variable that keeps track of how many question the user got right but I'm stuck on setting up the variable to keep track of the right answers.

How can I set this up, so when user is done with is quiz they get a message like "You missed 50 % of the questions " or "Congratulations.You answered all questions correctly". I appreciate any help or advice

    static void Main(string[] args)
    {
        GetQuestionCount();

    }

    public static int GetQuestionCount()
    {
        Console.Write("How many questions do want?");
        int numberQuestion = Convert.ToInt32(Console.ReadLine());
        int i = 0;
        do
        {
            if (numberQuestion >= 1)
            {
                DisplayQuestionMessage();
                i++;
            }
            else
            {
                Console.WriteLine("You Must Pick Atleast One Question");
            }
        } while (i < numberQuestion);

        return numberQuestion;
    }

    public static int DisplayProblem()
    {
        Random rnd = new Random();
        int numberOne = rnd.Next(0, 99);
        int numberTwo = rnd.Next(0, 99);
        int coin = rnd.Next(0, 2);
        if (coin == 0)
        {
            Console.WriteLine("{0} + {1} = ?", numberOne, numberTwo);
            int add = numberOne + numberTwo;
            return add;
        }
        else if (coin == 1)
        {
            if (numberOne >= numberTwo)
            {
                Console.WriteLine("{0} - {1} = ?", numberOne, numberTwo);
                return numberOne - numberTwo;
            }
            else
            {
                Console.WriteLine("{0} - {1} = ?", numberTwo, numberOne);
                return numberTwo - numberOne;
            }
        }

        return numberOne;
    }

    public static int GetUserGuess()
    {
        int userGuess = Convert.ToInt32(Console.ReadLine());
        return userGuess;
    }

    public static bool CheckAnswer(int CorrectAnswer, int UserGuess)
    {
        if (CorrectAnswer == UserGuess)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static void DisplayQuestionMessage()
    {
        Random rnd = new Random();
        int numberOne = rnd.Next(0, 99);
        int numberTwo = rnd.Next(0, 99);
        int coin = rnd.Next(0, 2);

        if (CheckAnswer(DisplayProblem(), GetUserGuess()))
        {
            Console.WriteLine("Your answer is correct");

        }
        else
        {

            if (coin == 0)
            {
                int add = numberOne + numberTwo;
                Console.WriteLine("Sorry,the right answer is {0}",add);
            }
            else if (coin == 1)
            {
                if (numberOne >= numberTwo)
                {
                    int sub1 = numberOne - numberTwo;
                    Console.WriteLine("Sorry,the right answer is {0}",sub1);
                }
                else
                {
                    int sub2 = numberTwo - numberOne;
                    Console.WriteLine("Sorry,the right answer is {0}",sub2);
                }
            }


        }
    }
    public static int CalculatePercentage(int correctQuestions, int numberOfQuestions)
    {

        //This method should calculate a percentage of correct answers out of all questions asked. Do not
        //output anything in this method.
        return correctQuestions;

    }
    public static void DisplayQuizMessage(int PercetageCorrect)
    {
       //This method should display the percentage of incorrect questions, or a message like, “Congratulations.
       //You answered all questions correctly.” 
    }
}

TaW's comment alludes to the fact that one integer divided by another in this scenario will probably be zero.

If the user answers 99 out of 100 questions correctly, the sum 99/100 is 0 when carried out with integers. To get round this and make it 0.99 etc, cast to (or use a) double when you do the sum

You're going to want a static double correctAnswers = 0; near the top of the class. Ensure you init it to 0. If you ever make the game restartable without running the program again, make sure you init it when you asj the user how many questions they want

Every time there is a correct answer, make sure you increment the correctAnswers counter

At the end of the questions round (probably in the Main()) calculate the percentage and then pass it to DisplayQuizMessage

This means you probably want a main that looks like:

static void Main(string[] args)
{
    int numQs = GetQuestionCount(); //will ask user for num of Qs and then also ask them

    int perc = CalculatePercentage(correctAnswers, numQs); //change the method signature so correctAnswers is a double

    DisplayQuizMessage(perc);

}

CalculatePercentage needs implementing using the formula you posted in your question. Cast to an int when you return

If you're not allowed to change correctAnswers parameter in CalculatePercentage from int to double then you can get the same effect by casting in your formula

double perc = (100.0 * correctAnswers)/totalQuestions;

The 100.0 instead of 100 means it will be compiled as a double rather than an int, so the whole calc will be carried out using doubles. You'll have to cast perc to an int when you return it, if CalculatePercentage must return an integer


The reason I haven't just out and out done it all for you is that this is clearly homework and you're supposed to get the learning benefit from doing it yourself. If you implement the steps outlined above, you'll get to where you're doing.

You have the formula correct but since integer division will truncate decimals, we need to multiply by 100 first, and then do the division. Otherwise we end up with a fraction that's less than 1 , which becomes 0 (for example, if we have 50 questions and answer 25 of them correctly, 25 / 50 =.5 , which becomes 0 as an integer. however if we do 100 * 25 / 50 , then we have 2500 / 50 , which results in 50% )

So the formula should look like:

public static int GetPercentCorrect(int correctAnswers, int numberOfQuestions)
{
    return 100 * correctAnswers / numberOfQuestions;
}

"How can I set this up, so when user is done with is quiz they get a message like "You missed 50 % of the questions " or "Congratulations.You answered all questions correctly""

As far as giving a specific message based on the percent correct, a common way to do that is to use if statements, starting at one end of the range, say where percentCorrect == 100 , and then working downwards:

public static void DisplayQuizMessage(int percentCorrect)
{
    if (percentCorrect == 100)
        Console.WriteLine("Congratulations, you got 100% correct. Grade: A+");
    else if (percentCorrect > 89)
        Console.WriteLine($"You did very well, with {percentCorrect}% correct. Grade: A");
    else if (percentCorrect > 79)
        Console.WriteLine($"You did well, with {percentCorrect}% correct. Grade: B");
    else if (percentCorrect > 69)
        Console.WriteLine($"You did average, with {percentCorrect}% correct. Grade: C");
    else if (percentCorrect > 59)
        Console.WriteLine($"You did poorly, with {percentCorrect}% correct. Grade: D");
    else
        Console.WriteLine($"You failed with {percentCorrect}% correct. Grade: F");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM