当前上下文中不存在名称(变量)
The name (variable) does not exist in the current context
我的程序中有一个错误,我似乎无法解决 - 即使在查看 Stack Overflow 和 Google 上的问题后也是如此。我的程序抛出异常,"The name (variable) does not exist in the current context."
do {
Console.WriteLine("");
Console.WriteLine("Before you begin, chose a battle to join!");
Console.WriteLine("1. Battle of Copenhagen - 1801");
Console.WriteLine("2. Battle of Morton's Ford - 1864");
Console.WriteLine("3. Battle of Schuinshoogte - 1881");
Console.WriteLine("4. Battle of Kufit - 1885");
Console.WriteLine("5. Invasion of Normandy - 1944");
String stringWarOption = Console.ReadLine();
int warOption = Convert.ToInt32(stringWarOption);
switch (warOption){
case 1:
//continue all code here
Console.WriteLine("Loading the Battle of Copenhagen - 1801.");
break;
case 2:
Console.WriteLine("This war is locked, however you can read up about it here: ");
Console.WriteLine("https://en.wikipedia.org/wiki/List_of_battles_1801%E2%80%931900");
break;
case 3:
Console.WriteLine("This war is locked, however you can read up about it here: ");
Console.WriteLine("https://en.wikipedia.org/wiki/List_of_battles_1801%E2%80%931900");
break;
case 4:
Console.WriteLine("This war is locked, however you can read up about it here: ");
Console.WriteLine("https://en.wikipedia.org/wiki/List_of_battles_1801%E2%80%931900");
break;
case 5:
Console.WriteLine("This war is locked, however you can read up about it here: ");
Console.WriteLine("https://en.wikipedia.org/wiki/Invasion_of_Normandy");
break;
default:
break;
}
}
while (warOption != 1);
您在 do
块内声明 warOption
,但在 while
语句的 do
块外引用它:
do {
/* ... */
int warOption = Convert.ToInt32(stringWarOption);
/* ... */
} while (warOption != 1);
该变量未在该范围内定义。将 warOption
的声明移动到 do
块之前:
int warOption;
do {
/* ... */
warOption = Convert.ToInt32(stringWarOption);
/* ... */
} while (warOption != -1);
我的程序中有一个错误,我似乎无法解决 - 即使在查看 Stack Overflow 和 Google 上的问题后也是如此。我的程序抛出异常,"The name (variable) does not exist in the current context."
do {
Console.WriteLine("");
Console.WriteLine("Before you begin, chose a battle to join!");
Console.WriteLine("1. Battle of Copenhagen - 1801");
Console.WriteLine("2. Battle of Morton's Ford - 1864");
Console.WriteLine("3. Battle of Schuinshoogte - 1881");
Console.WriteLine("4. Battle of Kufit - 1885");
Console.WriteLine("5. Invasion of Normandy - 1944");
String stringWarOption = Console.ReadLine();
int warOption = Convert.ToInt32(stringWarOption);
switch (warOption){
case 1:
//continue all code here
Console.WriteLine("Loading the Battle of Copenhagen - 1801.");
break;
case 2:
Console.WriteLine("This war is locked, however you can read up about it here: ");
Console.WriteLine("https://en.wikipedia.org/wiki/List_of_battles_1801%E2%80%931900");
break;
case 3:
Console.WriteLine("This war is locked, however you can read up about it here: ");
Console.WriteLine("https://en.wikipedia.org/wiki/List_of_battles_1801%E2%80%931900");
break;
case 4:
Console.WriteLine("This war is locked, however you can read up about it here: ");
Console.WriteLine("https://en.wikipedia.org/wiki/List_of_battles_1801%E2%80%931900");
break;
case 5:
Console.WriteLine("This war is locked, however you can read up about it here: ");
Console.WriteLine("https://en.wikipedia.org/wiki/Invasion_of_Normandy");
break;
default:
break;
}
}
while (warOption != 1);
您在 do
块内声明 warOption
,但在 while
语句的 do
块外引用它:
do {
/* ... */
int warOption = Convert.ToInt32(stringWarOption);
/* ... */
} while (warOption != 1);
该变量未在该范围内定义。将 warOption
的声明移动到 do
块之前:
int warOption;
do {
/* ... */
warOption = Convert.ToInt32(stringWarOption);
/* ... */
} while (warOption != -1);