很抱歉,如果这是一个重复的问题,或者听起来很愚蠢,但是我真的是C#的新手,在整个论坛上看了一下,找不到我能真正理解的东西。
因此,我试图编写一个简单的程序,让用户尝试猜测一个介于1到25之间的数字。除了循环的每次运行,而不是从循环的最后一次更新分数(例如0)起,一切正常+ 1 = 1、1 + 1 = 2、2 + 1 = 3,每次加1到0。这是我的代码。我该如何解决?谢谢!
int score = 0;
int add = 1;
while (add == 1)
{
Console.WriteLine("Guess A Number Between 1 and 25");
string input = Console.ReadLine();
if (input == "18")
{
Console.WriteLine("You Did It!");
Console.WriteLine("Not Bad! Your Score was " + score + add);
break;
}
else
{
Console.WriteLine("Try Again. Score: " + score + add);
}
}
答案 0 :(得分:5)
您实际上需要将add
添加到score
。尝试这样的事情:
int score = 0;
int add = 1;
while (add == 1)
{
Console.WriteLine("Guess A Number Between 1 and 25");
string input = Console.ReadLine();
score += add; // add `add` to `score`. This is the same as `score = score + add;`
if (input == "18")
{
Console.WriteLine("You Did It!");
Console.WriteLine("Not Bad! Your Score was " + score);
break;
}
else
{
Console.WriteLine("Try Again. Score: " + score);
}
}