代码无法计算百分比

时间:2016-12-24 16:05:11

标签: c# visual-studio console-application

感谢所有答案,这真的帮助了我!

但是我遇到了一个新问题......在我写的程序中,要求用户键入不同数学问题的答案,并且控制台会在每个问题后说出答案是正确还是不正确。最后,程序将显示有多少用户答案的​​百分比是正确的,这是我遇到了问题。

Console.Write("How many questions would you like to answer (Maximum value is '100'): ");
        string input = Console.ReadLine();
        int questionsAmount = int.Parse(input);

        int correctAnswers = 0;
        int IncorrectAnswers = 0;

        for (int i = 1; i <= questionsAmount; i++)
        {
            Console.WriteLine("===================");
            Console.WriteLine("Question " + i);
            Console.WriteLine();

            Random rnd = new Random();
            int rndNumber = rnd.Next(1, 100);
            int rndNumber2 = rnd.Next(1, 100);

            Console.Write("What equals " + rndNumber + " + " + rndNumber2 + " = ");
            string input2 = Console.ReadLine();
            int answer = int.Parse(input2);

            if (answer == rndNumber + rndNumber2)
            {
                correctAnswers++;
                Console.WriteLine("Answer correct.");
                Console.WriteLine("Correct answers: " + correctAnswers);
            }
            else
            {
                IncorrectAnswers++;
                Console.WriteLine("Answer incorrect.");
                Console.WriteLine("Incorrect answers: " + IncorrectAnswers);
            }
        }
        double percentage = correctAnswers / questionsAmount * 100;
        Console.WriteLine(percentage + "% of your answers was correct");

如您所见,一旦所有问题都得到解答,它将计算百分比,但由于我已经分配了变量&#39; correctAnswers&#39;对于值&#39; 0&#39;,计算将如下所示:0 / x * 100 = 0

如果我把变量&#39; correctAnswers&#39;和&#39;不正确的答案&#39;在for-statementdouble percentage = correctAnswers / questionsAmount * 100;内行不通。

4 个答案:

答案 0 :(得分:2)

只有在抛出异常时才会执行catch子句。你想要的是提出错误答案&#34;普通 while (true) { try { Console.Write("What equals " + rndNumber1 + " + " + rndNumber2 + ": "); string input = Console.ReadLine(); int answer = int.Parse(input); if (answer == rndNumber1 + rndNumber2) { Console.WriteLine("Your answer is correct."); break; } else { Console.WriteLine("Your answer is incorrect. Try again."); Console.ReadLine(); } } catch { { Console.WriteLine("Sorry, something went wrong."); } } } 子句中的逻辑。

bitwise and

答案 1 :(得分:1)

您正在寻找else块,而不是catch块:

while (true)
{
    Console.Write("What equals " + rndNumber1 + " + " + rndNumber2 + ": ");
    string input = Console.ReadLine();
    int answer = int.Parse(input);

    if (answer == rndNumber1 + rndNumber2)
    {
        Console.WriteLine("Your answer is correct.");
        break;
    }
    else
    {
        Console.WriteLine("Your answer is incorrect. Try again.");
    }
}

Try / catch用于处理例外,而非应用程序逻辑。 if elsecatch配对,而不与int.Parse()配对。

例如,如果您尝试catch一个不是整数的值(例如一串字母),那么 将抛出一个异常,你可以在int.TryParse()块中处理 。 (或使用 <div class="flextable table-actions"> <div class="flextable-item" style="padding-right:5px;"> <div class="btn-toolbar-item input-with-icon"> <button type="button" class="btn btn-xs btn-primary-outline" data-target="#addBudgetOriginal" data-toggle="modal">Original Budget</button> </div> </div> <div class="flextable-item" style="padding-right:5px;"> <div class="btn-toolbar-item input-with-icon"> <button type="button" class="btn btn-xs btn-primary-outline" data-target="#myModal" data-toggle="modal">Approved Budget Change</button> </div> </div> <div class="flextable-item" style="padding-right:5px;"> <div class="btn-toolbar-item input-with-icon"> <button type="button" class="btn btn-xs btn-primary-outline" data-target="#addBudget" data-toggle="modal">Budget Reallocation</button> </div> </div> <div class="flextable-item flextable-primary"> <div class="btn-toolbar-item input-with-icon"> <button type="button" class="btn btn-xs btn-primary-outline" data-target="#addBudget" data-toggle="modal">Pending Budget Reduction</button> </div> </div> 来避免异常。)

答案 2 :(得分:1)

放置可能在try块中抛出异常的代码部分,并放置处理catch块中异常的代码。 catch块是一系列以关键字catch开头的语句,后跟异常类型和要采取的操作 您可以输出错误答案&#34;提示在else部分,如下所示。 尝试并捕获statemnts是为了执行代码行异常。你正在测试的是代码执行中的异常。它只是一个不等于另一个的变量。

 {
            Random rnd = new Random();
            int rndNumber1 = rnd.Next(1, 100);
            int rndNumber2 = rnd.Next(1, 100);

            while (true)
            {
                try
                {
                    Console.Write("What equals " + rndNumber1 + " + " + rndNumber2 + ": ");
                    string input = Console.ReadLine();
                    int answer = int.Parse(input);

                    if (answer == rndNumber1 + rndNumber2)
                    {
                        Console.WriteLine("Your answer is correct.");
                        break;
                    }else{
     Console.WriteLine("Your answer is incorrect. Try again.");
    }
                }
                catch
                {

                }
            }
        }

答案 3 :(得分:0)

try...catch控制块用于处理exeptions,这意味着当加密意外错误时代码将跳转到cath部分。 在您的代码中,只有在抛出运行时错误时,才会执行此部分Console.WriteLine("Your answer is correct.");int.Parse()无法解析该值。

当您尝试比较值时,应使用if流结构

{
    Random rnd = new Random();
    int rndNumber1 = rnd.Next(1, 100);
    int rndNumber2 = rnd.Next(1, 100);

    while (true)
    {
            Console.Write("What equals " + rndNumber1 + " + " + rndNumber2 + ": ");
            string input = Console.ReadLine();
            int answer = int.Parse(input);

            if (answer == rndNumber1 + rndNumber2)
            {
                Console.WriteLine("Your answer is correct.");
                break;
            }
            else
            {
                Console.WriteLine("Your answer is incorrect. Try again.");
            }
    }
}