gpt4 book ai didi

c#简单评分系统

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:05 25 4
gpt4 key购买 nike

我正在制作一个简单的游戏。当玩家的答案正确时,我试图给他 +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;
}
}

最佳答案

1) 来自 C# 规范 ( MSDN )

Furthermore, a variable initializer in a local variable declaration corresponds exactly to an assignment statement that is inserted immediately after the declaration.

换句话说,每次您的程序进入start 赋值语句时,score 都会初始化为0

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

    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因为

  • 它使代码困惑
  • 降低可读性

这个article关于 GO TO 对你来说可能很有趣。

我建议更换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);
}
}

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!");
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);
}

4) 你的代码包含 bug 如果你只想在答案正确时增加分数,你应该从 else block 中删除++score。

5) 不要复制代码 如您所见,if block 和else block 中的最后两个语句是相同的。我建议将它们从 if else 运算符中移出:

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!");

非常相似(但不一样!!!)
            Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Bummer, try again!");

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

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);
}

关于c#简单评分系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39419056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com