我正在尝试将一个 Enemy 敌人类实例分配给一个随机的 newenemy 实例。例如:
public Enemy GetEnemy()
{
Random rand = new Random();
Enemy newenemy = new Enemy(string.Empty, 0, 0, 0, 0, 0);
if (Main.level == 1)
{
int z = rand.Next(0, 2);
if (z == 0)
{
newenemy = new Enemy("Chicken", 50, 0, 4, 11, 32);
}
else if (z == 1)
{
newenemy = new Enemy("Common Goblin", 67, 0, 8, 14, 36);
}
else if (z == 2)
{
newenemy = new Enemy("Rabbit", 50, 0, 15, 25, 9);
}
}
return newenemy;
}
然后在我的战斗功能中使用它:(我会发布一些因为它有点长)
if (whathappen == 2 || whathappen == 4 || whathappen == 6 || whathappen == 8 || whathappen == 10) //battle code
{
Console.Write("You were engaged by the enemy! \nYou are fighting an enemy {0}\n", GetEnemy().name);
while (Main.health > 0 || GetEnemy().health > 0)
{
Console.Write("1) Attack With Weapon ");
int choose;
int.TryParse(Console.ReadLine(), out choose);
if (choose == 1)
{
int miss = r.Next(0, 6);
if (miss < 6)
{
int totaldamage = (r.Next(0, Main.RHand.damage) + r.Next(0, Main.LHand.damage)) - GetEnemy().armor;
GetEnemy().health -= totaldamage;
Console.WriteLine("You attack the {0} with a {1} and deal {2} damage!", GetEnemy().name, Main.RHand.name, totaldamage);
Console.WriteLine("The {0} has {1} health remaning!", GetEnemy().name, GetEnemy().health);
但是,当我测试我的游戏时,战斗中没有分配敌人,我不明白为什么。这是输出:
你被敌人搞定了!你在和敌人作战!(敌人的名字应该在敌人之后)
你用青铜匕首攻击(名字应该再次出现)并造成 15 点伤害!
谁能解释为什么会这样?