【发布时间】:2013-03-24 12:14:16
【问题描述】:
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 循环的范围。如果它不是范围问题,那是我的幸运猜测。在那种情况下出了什么问题,移动那行代码是如何解决的?
【问题讨论】: