我是编码新手,我写的代码有问题,我正在编写一个使用随机字符串元素的地牢爬虫:
string[] enemies = {"Ghost", "Skeleton", "ogre", "slime", "zombie", "pig", "bush"};
Console.WriteLine("\nLevel " + level);
Console.WriteLine("A wild " + enemies[i] + " Appears");
我将“i”设置为列表,然后我询问玩家的动作是什么:
> Console.WriteLine("\nYour action: " + "\nA = Attack \nD = Deffend \nI = Items \nC = Check your and enemies stats");
但是每次我回答时,“i”int 都会更改为不同的数字,有没有办法将“i”int 设置为每次我回答时都不会改变?
这里是整个代码:
using System;
public class Program
{
public static void Main()
{
Random rnd = new Random();
string[] enemies = {"Ghost", "Skeleton", "ogre", "slime", "zombie", "pig", "bush"};
string[] Items = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
int level = 1;
int health = 100;
int armour = 10;
int damage = 10;
int EHealth = 0;
int EArmour = 0;
int EDamage = 0;
int D;
int YD;
int OneOrTwo;
int i = rnd.Next(0, 7);
string R = "bla";
Console.WriteLine("\nLevel " + level);
Console.WriteLine("A wild " + enemies[i] + " Appears");
for(health = 100; health > 0; level++)
{
if(i == 1)//skeleton
{
EHealth = 5 * level; EArmour = level * 3; EDamage = level * 3;
}else if(i == 2)//ogre
{
EHealth = 8 * level; EArmour = level * 5; EDamage = level * 6;
}else if(i == 0)//ghost
{
EHealth = 5 * level; EArmour = level * 2; EDamage = level * 4;
}else if(i == 3)//slime
{
EHealth = 4 * level; EArmour = level * 6; EDamage = level * 2;
}else if(i == 4)//zombie
{
EHealth = 6 * level; EArmour = level * 4; EDamage = level * 3;
}else if(i == 5)//pig
{
if(level > 2)
{
EHealth = 4 * level; EArmour = 2 * (level - 2); EDamage= 0;
}else
{
EHealth = 4 * level; EArmour = 0; EDamage= 0;
}
}else // bush
{
EHealth = 1 * level; EArmour = 0; EDamage= 0;
}
Console.WriteLine("\nEnemy health = " + EHealth + "\nEnemy armour = " + EArmour + "\nEnemy damage = " + EDamage);
Console.WriteLine("\nYour health = " + health + "\nYour armour = " + armour + "\nYour damage = " + damage);
Console.WriteLine("\nYour action: " + "\nA = Attack \nD = Deffend \nI = Items \nC = Check your and enemies stats");
for(R = Console.ReadLine(); EHealth >= 0; R = Console.ReadLine())
{
if(R == "A")
{
Console.WriteLine("You grab your sword preparing to slash");
}else if(R == "D")
{
Console.WriteLine("\nYou lift up your shield");
}else if(R == "I")
{
Console.WriteLine("\nYou open your inventory");
}else if(R == "C")
{
Console.WriteLine("Enemy health = " + EHealth + "\nEnemy armour = " + EArmour + "\nEnemy damage = " + EDamage);
Console.WriteLine("\nYour health = " + health + "\nYour armour = " + armour + "\nYour damage = " + damage);
continue;
}else
{
Console.WriteLine("\nWrong input, try again");
continue;
}
OneOrTwo = rnd.Next(0, 2);
if (OneOrTwo == 0)
{
D = EDamage;
if(R == "D")
{
D = armour - EDamage;
if(D > -1)
{
D = 0;
}else
{
Math.Abs(D);
}
Console.WriteLine("The monster attacks doing " + D + " Damage.");
health = health - D;
}else
{
Console.WriteLine("The monster attacks doing " + D + " Damage.");
Console.WriteLine("You attack the monster doing " + damage + " Damage.");
health = health - D;
EHealth = EHealth - damage;
}
}else
{
Console.WriteLine("The monster's shilds up");
if(R == "D")
{
Console.WriteLine("You also put your shield up... nothing happened.");
}else
{
YD = EArmour - damage;
if(YD > -1)
{
YD = 0;
Console.WriteLine("You attack the monster doing " + YD + " damage.");
EHealth = EHealth - YD;
}else
{
//Math.Abs(YD);
Console.WriteLine("You attack the monster doing " + YD + " damage.");
EHealth = EHealth - YD;
}
//Console.WriteLine("You attack the monster doing " + YD + " damage.");
//EHealth = EHealth - YD;
continue;
}
}
}
}
Console.WriteLine("Game Over");
return;
}
}