c#简单评分系统

时间:2016-09-09 20:10:55

标签: c#

我正在做一个简单的游戏。当他的答案是正确的时候,我试图给予玩家+1分数,但它一直说同样的分数1.我希望在你的答案正确时不断更新分数。

因此,如果你有两个正确的答案,那么分数应该更新为2,但它不会并且一直说1 ...

        start:

        Random numbergenerator = new Random ();

        int num1 = numbergenerator.Next(1,11);
        int num2 = numbergenerator.Next(1,11);
        int score = 0; // THIS IS THE SCORE

        Console.WriteLine("Whats " + num1 + " times " + num2 + "?");

        var answer = Convert.ToInt32(Console.ReadLine());

        if ( answer == num1 * num2) {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Thats the correct answer!");
            Console.ResetColor();
            ++score; // Gives score
            Console.WriteLine("Your score: " + score);
        } else {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Bummer, try again!");
            Console.ResetColor();
            ++score; // Gives score
            Console.WriteLine("Your score: " + score);
        }
        goto start;
    }
}

}

5 个答案:

答案 0 :(得分:2)

每次跳跃goto start;后,您的分数设置为0。将您的得分声明置于start:以上,如下所示:

 int score = 0; // THIS IS THE SCORE
 start:
 ...other instructions

请记住,无论您使用goto还是while,如果变量必须在循环期间存储状态,请在该循环之外声明并初始化它。

答案 1 :(得分:1)

每次goto start时,您都会将得分设为0.

你可以停止使用goto吗?它伤害了我的眼睛,我相信许多其他的眼睛。请改用while

如果顺便使用while,问题将会消失,因为每次迭代都不会重置分数。

答案 2 :(得分:1)

  1. 不要使用goto这是不好的做法,你应该使用循环。
  2. 您的代码应如下所示:

            Random numbergenerator = new Random();
    
            int score = 0; // THIS IS THE SCORE
    
            while (true)
            {
                int num1 = numbergenerator.Next(1, 11);
                int num2 = numbergenerator.Next(1, 11);
    
                Console.WriteLine("Whats " + num1 + " times " + num2 + "?");
    
                var answer = Convert.ToInt32(Console.ReadLine());
    
                if (answer == num1 * num2)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Thats the correct answer!");
                    Console.ResetColor();
                    ++score; // Gives score
                    Console.WriteLine("Your score: " + score);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Bummer, try again!");
                    Console.ResetColor();
                    Console.WriteLine("Your score: " + score);
                }
            }
    

答案 3 :(得分:1)

1)来自C#规范(MSDN

  

此外,局部变量声明中的变量初始值设定项与声明后立即插入的赋值语句完全对应。

换句话说,每当您的程序转到start赋值语句时,score0

我建议在 start 之前移动初始化(最好还是移动numbergenerator 初始化。):

    Random numbergenerator = new Random ();
    int score = 0; // THIS IS THE SCORE

    start:

    int num1 = numbergenerator.Next(1,11);
    int num2 = numbergenerator.Next(1,11);

    Console.WriteLine("Whats " + num1 + " times " + num2 + "?");

    var answer = Convert.ToInt32(Console.ReadLine());

    if ( answer == num1 * num2) {
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("Thats the correct answer!");
        Console.ResetColor();
        ++score; // Gives score
        Console.WriteLine("Your score: " + score);
    } else {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Bummer, try again!");
        Console.ResetColor();
        ++score; // Gives score
        Console.WriteLine("Your score: " + score);
    }
    goto start;

2)不要使用goto因为

  • 它使代码混乱
  • 降低了可读性

这个关于GO TO的article对你来说很有意思。

我建议更换 start while (true){ goto start; } Random numbergenerator = new Random (); int score = 0; // THIS IS THE SCORE while(true) { int num1 = numbergenerator.Next(1,11); int num2 = numbergenerator.Next(1,11); Console.WriteLine("Whats " + num1 + " times " + num2 + "?"); var answer = Convert.ToInt32(Console.ReadLine()); if ( answer == num1 * num2) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Thats the correct answer!"); Console.ResetColor(); ++score; // Gives score Console.WriteLine("Your score: " + score); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Bummer, try again!"); Console.ResetColor(); ++score; // Gives score Console.WriteLine("Your score: " + score); } } public void TryToGuessMultiplication_GameStep(int num1, int num2) { Console.WriteLine("Whats " + num1 + " times " + num2 + "?"); var answer = Convert.ToInt32(Console.ReadLine()); if ( answer == num1 * num2) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Thats the correct answer!"); Console.ResetColor(); ++score; // Gives score Console.WriteLine("Your score: " + score); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Bummer, try again!"); Console.ResetColor(); ++score; // Gives score Console.WriteLine("Your score: " + score); } } 它应该看起来像这样:

Random numbergenerator = new Random ();
int score = 0; // THIS IS THE SCORE

while(true)
{
    int num1 = numbergenerator.Next(1,11);
    int num2 = numbergenerator.Next(1,11);
    TryToGuessMultiplication_GameStep(int num1, int num2);      
}

3)提取方法,它并不重要,但我建议通过提取方法来提高可读性。此方法将包含循环体。代码看起来应该是这样的:

public void TryToGuessMultiplication_GameStep(int num1, int num2)
{
            Console.WriteLine("Whats " + num1 + " times " + num2 + "?");

            var answer = Convert.ToInt32(Console.ReadLine());

            if ( answer == num1 * num2) {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Thats the correct answer!");
                ++score; // Gives score
            } else {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Bummer, try again!");
            }
            Console.ResetColor();
            Console.WriteLine("Your score: " + score);
}

...

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Thats the correct answer!");

4)您的代码包含错误如果您只想在aswer正确时增加分数,则应从 else 块中删除++分数。

5)不要复制代码正如您可以看到中的最后两个语句,如果块和 else 块相同。我建议将它们从 if else 运算符移出:

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Bummer, try again!");

并非全部。现在你可以看到

public void PrintUserMessage(ConsoleColor color, string message)
{
        Console.ForegroundColor = color;
        Console.WriteLine(message);
}

public void TryToGuessMultiplication_GameStep(int num1, int num2)
{
            Console.WriteLine("Whats " + num1 + " times " + num2 + "?");

            var answer = Convert.ToInt32(Console.ReadLine());

            if ( answer == num1 * num2){
                PrintUserMessage( ConsoleColor.Green, "Thats the correct answer!");
                ++score; // Gives score
            }else
                PrintUserMessage( ConsoleColor.Red,"Bummer, try again!");

            Console.ResetColor();
            Console.WriteLine("Your score: " + score);
}

非常相似(但不一样!!!)

docker-machine create --driver generic ...

我们无法通过将其移出 if else 运算符来改进我们的代码。但有一个技巧 - 我们可以提取方法,我们将减少代码行:

docker-machine create --driver generic ...

答案 4 :(得分:0)

添加其他答案,因为您只想在用户获得正确答案时提高分数,您应该从s3a://语句中删除++score;行,就像您目前的方式一样无论答案是否正确,分数都会增加。