当我选择在这段代码中运行时,我得到一个空引用异常。
var monster = map[player.YPosition, player.XPosition].MonsterEncounter.Monster.MonsterName;
Console.WriteLine("You encounter a {0}.", monster);
var run = false;
while (map[player.YPosition, player.XPosition].MonsterEncounter.Monster.Health > 0 || !run)
{
Console.WriteLine("What your next move? Fight or Run?");
var choice = Console.ReadLine().ToLower();
switch (choice)
{
case "fight":
Attack(player, map[player.YPosition, player.XPosition].MonsterEncounter.Monster);
break;
case "run":
run = true;
Movement(player, map[player.YPosition, player.XPosition]);
break;
}
}
而且我知道为什么,因为当我跑到没有怪物的瓷砖时,while循环的健康状况为空。然而,我无法想到解决这个问题的方法。任何人都可以帮助我吗?