gpt4 book ai didi

C# - 该名称在当前上下文中不存在

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:41 31 4
gpt4 key购买 nike

我这周开始学习 C#,但在完成这项作业时遇到了麻烦。
他们让我们买的那本昂贵的书并没有很好地解释这一点。
我们必须在这个作业中使用 If、Else、While 和 Do。

我收到此错误,名称“answer”在当前上下文中不存在。
我做错了什么?

int question = 25;

do
{
Console.Write("Guess the number: ");
string strAnswer = Console.ReadLine();
int answer = Convert.ToInt32(strAnswer);

if (answer < question)
{
Console.WriteLine("Too low, guess again. ");
}
else if (answer > question)
{
Console.WriteLine("Too high, guess again. ");
}
}

while (answer != question);
Console.Write("Correct!");

最佳答案

试试这个,在不同的范围定义答案,你需要在更高的范围定义它,

 int question = 25;
int answer = 0;
do
{
Console.Write("Guess the number: ");
string strAnswer = Console.ReadLine();
answer = Convert.ToInt32(strAnswer);

if (answer < question)
{
Console.WriteLine("Too low, guess again. ");
}
else if (answer > question)
{
Console.WriteLine("Too high, guess again. ");
}
}

while (answer != question);
Console.Write("Correct!");

关于C# - 该名称在当前上下文中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228434/

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