下面的代码是适用于C#中的城堡迷宫游戏的工作代码段。
if else结构只能正确打印dun.roomend == true)。现在,应在显示tre.isExit时显示tow.roomEnd。 tre.isExit根本不显示。
我将当前变量声明为:
public bool isExit;
public bool deadEnd;
public bool roomEnd;
tre.isExit = true;
dun.deadEnd = true;
tow.roomEnd = true;
if (dun.roomEnd == true)
{
Console.WriteLine("You've fallen into the Dungeons of the Dead. Try again");
return;
}
if (tow.roomEnd == true)
{
Console.WriteLine("You been caught by the Kings guard and have been placed in the tower for life.Try again");
return;
}
else if (tre.isExit == true)
{
Console.WriteLine("You have found the treaure... now run!!");
return;
}
else
{
Console.WriteLine("Too scared.....");
}
最佳答案
那是因为当您的条件之一为真时,您会立即返回。
// Don't explicitly compare to true - just write if (dun.roomEnd)
if (dun.roomEnd == true)
{
Console.WriteLine("You've fallen into the Dungeons of the Dead. Try again");
// You end the method here, so none of the rest of the code after this will execute
return;
}
else if (tre.isExit == true)
tow.roomEnd == true
if (A) {
// do work
}
else if (B) {
// Do work
}
if (A) {
// Do work
}
if (!A && B) {
// Do work
}
true
或false
,因此if (tow.roomEnd == true)
if (tow.roomEnd)
https://stackoverflow.com/questions/41106268/