Eli*_*ing 1 c# int if-statement while-loop
对不起,如果这是一个重复的问题或听起来很愚蠢,但我对 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);
}
}
Run Code Online (Sandbox Code Playgroud)
您需要实际添加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);
}
}
Run Code Online (Sandbox Code Playgroud)