这是我在 C# 2010 中创建的游戏,但是当用户输入 [1] 增加防御时,comp 的 Attack 和 Health 值变为负数。
bool jet = false;
int userinput;
Console.WriteLine("Pick your team color, Blue or Green. ");
string userName = Console.ReadLine();
if (userName == "blue")
{
user.Name = "Blue Fighterjet";
comp.Name = "Green Fighterjet";
}
else
{
user.Name = "Green Fighterjet";
comp.Name = "Blue Fighterjet";
}
Console.Clear();
Console.WriteLine("You are now the {0}.", user.Name);
Console.ReadLine();
Console.Clear();
Console.WriteLine("What do you want to do for your first move?");
Console.ReadLine();
while (jet == false)
{
Console.Clear();
Console.WriteLine("Your stats:");
Console.WriteLine("Health {0} \n Defense {1} \n Attack {2}", user.Health, user.Defense, user.Attack);
Console.WriteLine("Comp stats:");
Console.WriteLine("Health {0} \n Defense {2} \n Attack {2}", comp.Health, comp.Defense, comp.Attack);
Console.ReadLine();
Console.WriteLine("[1] Increase your Defense.");
Console.WriteLine("[2] Regain some of your Health.");
Console.WriteLine("[3] Attack the {0}.", comp.Name);
userinput = int.Parse(Console.ReadLine());
if (userinput == 1)
{
user.Defense = user.Defense + random.Next(1, 10);
if (userinput == 1)
{
comp.Attack = comp.Attack - user.Defense + random.Next(1, 10);
}
}
if (userinput == 2)
{
user.Health = user.Health + random.Next(1, 10);
if (userinput == 2)
{
comp.Defense = comp.Defense + random.Next(1, 10);
}
}
if (userinput == 3)
{
user.Attack = user.Attack + random.Next(1, 10);
if (userinput == 3)
{
comp.Health = comp.Health - user.Attack + random.Next(1, 10);
}
}
}
}
}
class jet
{
private string name;
private int health = 100;
private int defense = 0;
private int attack = 5;
public string Name
{
get { return name; }
set { name = value; }
}
public int Health
{
get
{
return health;
}
set
{
if (value <= 100)
{
health = value;
}
else
{
health = 100;
}
}
}
public int Defense
{
get
{
return defense;
}
set
{
if (value <= 10)
{
defense = value;
}
else
{
defense = 10;
}
}
}
public int Attack
{
get
{
return attack;
}
set
{
if (value <= 5)
{
attack = value;
}
else
{
attack = 5;
}
}
}
}
关于如何轻松解决此问题的任何提示?
我的用户输入不正确吗?