<分区>
C# 作业问题:我刚刚使用 do-while 循环添加了一些“再次播放”逻辑。这是我的原始代码:
namespace demo
{
class Program
{
static void Main(string[] args)
{
Info myInfo = new Info();
myInfo.DisplayInfo("Daniel Wilson", "4 - Hi-Lo Game");
// I moved String playAgain = "N"; to here
do
{
DWHiLowUI theUI = new DWHiLowUI();
theUI.Play();
String playAgain = "N";
Console.WriteLine("Enter 'Y' to play again, any other key to exit.");
playAgain = Console.ReadLine();
}
while ((playAgain == "Y")||(playAgain =="y"));
Console.ReadLine();
}
}
}
这给了我一个错误:
Error 7 The name 'playAgain' does not exist in the current context
我将 String playAgain = "N";
移动到我的 do
上方的行(见评论)并且它工作正常。
我正在尝试了解我做了 来解决这个问题。这似乎是一个范围问题,但在我看来,在循环中定义一个变量可以将它传递到循环的末尾。我浏览了我的教科书,没有任何关于范围的内容,因为它与循环有关。这对我来说意味着循环内的范围不是问题,但这表现得好像是范围问题。我自己都在想这件事。
如果它是范围问题,我想更好地理解方法中do...while
循环的范围。如果它不是范围问题,那是我的幸运猜测。在那种情况下,哪里出了问题,移动那行代码是如何解决问题的?