PlayerClass属性不改变if语句中的PlayerHealth属性

时间:2019-05-08 22:40:59

标签: c#

我为玩家创建了一个对象,使其具有健康状况以及玩家将要成为的职业。当我尝试更改if语句内部的health属性时,它不会更改

我尝试过重新分配值并将其添加到原始值

static void Main(string[] args)
        {
            Console.WriteLine("Hello What is your name?");

            Player player = new Player("", "", 0);
            player.Name = Console.ReadLine();
            string playerName = player.Name;

            Console.WriteLine($"Welcome {playerName} to my game. It's a simple text based adventure game");

            Console.WriteLine("What class would you like to be?");

            player.PlayerClass = Console.ReadLine();
            string sPlayerClass = player.PlayerClass;

            if (sPlayerClass.Contains("warrior"))
            {
                player.PlayerClass = "worrior";
                player.Health = player.Health + 20;
            }
            Console.WriteLine(player.Health);
            Console.WriteLine(player.PlayerClass);

这是来自主要班级

这是来自玩家类

public class Player
{

    public string Name {get; set;}
    public string PlayerClass{get; set;}
    public int Health{get; set;}

    public Player(string name, string playerClass, int health)
    {   
        Name = name;
        PlayerClass = playerClass;
        Health = health;
    }
}

预期:Console.Writeline(player.Health)= 20

实际:Console.Writeline(player.Health)= 0

1 个答案:

答案 0 :(得分:0)

安东尼奥·韦内罗索·孔特雷亚斯(Antonio Veneroso Contreas)需要将勇士变成勇士