好吧,我發現了一個無法訪問的代碼,而不是所有的代碼路徑從代碼可達代碼,而不是所有的代碼路徑返回一個值
private string MoveForward()
{
//if current direction is east, GetRoomEast
if (Player.CurrentDirection == "EAST")
{
return GetroomEast();
if (Player.NextMove != "")
{
Player.Y++;
}
else
{
Console.WriteLine("You Bumped into a Wall");
}
//if GetRoomEast is not "", then playercol = playercol+1;
//if current direction is west...
}
}
該塊返回值,我在上面初始化的變量是
public struct DragonPlayer
{
public int X, Y;
public string CurrentDirection;
public string NextMove;
}
public class DragonGameboard
{
public string[,] GameboardArray;
public DragonPlayer Player;
private Random r;
public DragonGameboard()
{
GameboardArray = new string[4, 4];
Player.CurrentDirection = "EAST";
Player.NextMove = "";
r = new Random();
Player.X = r.Next(0, 4);
Player.Y = r.Next(0, 4);
GenerateRandomBoard();
}
}
爲什麼這樣做?我確定它必須是非常愚蠢的東西,但我很難弄清楚它是什麼?
我的意思是我得到這些錯誤的頂部。 – user3196400
你嘗試過研究錯誤嗎?它幾乎總結了這一點:您不返回所有代碼路徑的值,而返回類型爲「string」,而「無法訪問的代碼」意味着該代碼將永遠不會到達,因爲您返回之前。 – CodeCaster
@CodeCaster使用Google的Beucase比要求堆棧溢出更難':)' –