计算特定数学方程式错误的次数,并在最后显示

时间:2014-11-09 15:15:49

标签: c#

基本上我已经创建了一个时间表应用程序。在应用程序结束时,我已经能够说出错了多少次,以及我说错了多少次。

但我希望它说:

你得到了:

6 x 4错6次。

5 x 2错9次。

等...

而不是你有20个正确,8个错误。

所以我可以看到错误的具体乘法。我知道我需要在哪里添加代码(在else语句下)。但不知道如何解决这个问题。

我认为最好的解决方案是将所有错误存储为数组中的字符串,然后计算相同的字符串并输出数字。但我不知道该怎么做。

这是我的代码:

namespace TimesTablesGame
{
class multiplication
{
    Random rng = new Random();
    int randomNumber;
    int randomNumberTables;
    int c = 0;
    int w = 0;
    int numberOfGos = 0;
    int minRangeTables;
    int maxRangeTables;
    int minRange;
    int maxRange;


    public multiplication()
    {
    start:
        Console.Clear();
        Console.WriteLine("\nPlease enter the lowest number range you would like to practice your times tables on: ");
        minRangeTables = int.Parse(Console.ReadLine());

        Console.WriteLine("\nPlease enter the higest number range you would like to practice your times tables on: ");
        maxRangeTables = int.Parse(Console.ReadLine());

        Console.WriteLine("\nPlease enter the number of times you would like play: ");
        numberOfGos = int.Parse(Console.ReadLine());

        Console.WriteLine("\nPlease enter the minimum range you would like to multiply by: ");
        minRange = int.Parse(Console.ReadLine());

        Console.WriteLine("\nPlease enter the maximum range you would like to multiply by: ");
        maxRange = int.Parse(Console.ReadLine());

    repeat:
        Console.Clear();


        for (int i = 1; i <= numberOfGos; i++)
        {


            randomNumberTables = rng.Next(minRangeTables, maxRangeTables + 1);
            randomNumber = rng.Next(minRange, maxRange + 1);



            Console.Write("\n\n{0}: {1} x {2} = ", i, randomNumberTables, randomNumber);

            if (randomNumberTables * randomNumber == int.Parse(Console.ReadLine()))
            {

                Console.WriteLine("Correct");
                c++;

            }
            else
            {
                Console.WriteLine("Wrong it is: " + randomNumberTables * randomNumber);

                w++;
            }
        }

        Console.WriteLine("\nYou were correct {0} times, and wrong {1} times.", c, w);

        Console.ReadLine();
        Console.WriteLine("Would you like to play again? Type y for Yes with new settings, r for repeat using last settings, and any other key to exit.");
        char again = Console.ReadKey().KeyChar;

        if (again == 'y')
        {
            c = 0;
            w = 0;
            goto start;
        }
        else if (again == 'r')
        {
            c = 0;
            w = 0;
            goto repeat;
        }

    }

}
}

1 个答案:

答案 0 :(得分:1)

我会在字典中跟踪......

抵制重写GOTO东西的冲动.....这是一个相对简单的方法:

Dictionary<string, int> wrongs;

//Console questions...

//Begin looping logic

wrongs = new Dictionary<string, int>();

for (int i = 1; i <= numberOfGos; i++)
{
    randomNumberTables = rng.Next(minRangeTables, maxRangeTables + 1);
    randomNumber = rng.Next(minRange, maxRange + 1);

    string eq = String.Format("{0} x {1} = ", randomNumberTables, randomNumber);

    Console.Write("{0}: " + eq, i);

    if (randomNumberTables * randomNumber == int.Parse(Console.ReadLine()))
    {
        Console.WriteLine("Correct");
        c++;
    }
    else
    {
        Console.WriteLine("Wrong it is: " + randomNumberTables * randomNumber);

        if (wrongs.Any(x => x.Key == eq))
        {
            wrongs[eq]++;
        }
        else
        {
            wrongs.Add(eq, 1);
        }

        w++;
    }
}

Console.WriteLine("\nYou were correct {0} times, and wrong {1} times.", c, w);

Console.WriteLine("\n\nYou got:");
foreach (var item in wrongs)
{
    Console.WriteLine("{0} wrong {1} times", item.Key, item.Value);
}

//Your logic to repeat/restart

另外....请注意,这不算“2 x 4”和“4 x 2”作为相同的等式....

另外....你可以轻松地在不使用GOTO的情况下做到这一点....请考虑一下。