我在显示对象的属性以及在类之间传输它们时遇到麻烦。我也无法从set方法获取错误消息。看来我可以输入任何东西而不会出错。我知道问题可能存在于对象中,因为尝试显示它时,我只是获得了空白的变量名。
namespace Project
{
class Program
{
static void Main(string[] args)
{
string name;
double strength = 0;
double dexterity = 0;
double hitPoints = 0;
double armor = 0;
Console.WriteLine("--WELCOME TO THE BATTLE--\n");
Console.WriteLine("Please enter the statistics for warrior one.");
Console.WriteLine("Name: ");
name = Console.ReadLine();
Console.WriteLine("Strength: ");
strength = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Dexterity: ");
dexterity = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Hit Points: ");
hitPoints = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Armor: ");
armor = Convert.ToDouble(Console.ReadLine());
Warrior warriorOne = new Warrior(name, strength, dexterity, hitPoints, armor);
Console.WriteLine("\nPlease enter the statistics for warrior two.");
Console.WriteLine("Name: ");
name = Console.ReadLine();
Console.WriteLine("Strength: ");
strength = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Dexterity: ");
dexterity = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Hit Points: ");
hitPoints = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Armor: ");
armor = Convert.ToDouble(Console.ReadLine());
Warrior warriorTwo = new Warrior(name, strength, dexterity, hitPoints, armor);
Console.WriteLine("\nWarriors:\n" + warriorOne.Name + "--" + "Strength " + warriorOne.Strength + ", Dexterity " + warriorOne.Dexterity + ", HitPoints " + warriorOne.HitPoints + ", Armor " + warriorOne.Armor);
Console.WriteLine("\nWarriors:\n" + warriorTwo.Name + "--" + "Strength " + warriorTwo.Strength + ", Dexterity " + warriorTwo.Dexterity + ", HitPoints " + warriorTwo.HitPoints + ", Armor " + warriorTwo.Armor);
Battlefield.Battle(warriorOne, warriorTwo);
}
}
class Warrior
{
private double strength;
private double dexterity;
private double hitPoints;
private double armor;
public Warrior(string name, double strength, double dexterity, double hitPoints, double armor)
{
name = Name;
this.strength = Strength;
this.dexterity = Dexterity;
this.hitPoints = HitPoints;
this.armor = Armor;
}
public string Name { get; set; }
public double Strength
{
get
{
return strength;
}
set
{
while (value < 10 & value > 20)
{
Console.WriteLine("Strength has a range of 10-20. Please re-enter: ");
value = Convert.ToDouble(Console.ReadLine());
}
strength = value;
}
}
public double Dexterity
{
get
{
return dexterity;
}
set
{
while (value < 10 & value > 20)
{
Console.WriteLine("Dexterity has a range of 10-20. Please re-enter: ");
value = Convert.ToDouble(Console.ReadLine());
}
dexterity = value;
}
}
public double HitPoints
{
get
{
return hitPoints;
}
set
{
while (value < 10 & value > 20)
{
Console.WriteLine("Hit points have a range of 10-20. Please re-enter: ");
value = Convert.ToDouble(Console.ReadLine());
}
hitPoints = value;
}
}
public double Armor
{
get
{
return armor;
}
set
{
while (value < 0 & value > 5)
{
Console.WriteLine("Armor has a range of 0-5. Please re-enter: ");
value = Convert.ToDouble(Console.ReadLine());
}
armor = value;
}
}
}
class Battlefield
{
public static void Battle(Warrior warriorOne, Warrior warriorTwo)
{ Console.WriteLine(warriorOne.Name); }
}
答案 0 :(得分:1)
在构造器中尝试设置Warrior
属性的Name
类是一个问题。你有这个(我只包括相关的位):
public Warrior(string name)
{
name = Name;
}
public string Name { get; set; }
请注意,您不在此处设置Name
属性。您正在将参数name
设置为Name
属性的任何一个(可能是null
)。
要解决此问题,只需切换作业的顺序即可:
public Warrior(string name)
{
Name = name;
}
您可能还需要检查所有其他属性(这似乎是一个常见错误,您要在{{ 1}},而不是 left )。
请记住,您几乎应该始终分配给公共访问者(大写的访问者),以便您的=
代码运行。尝试除修改属性set
方法之外的私有后备字段。