C# 如何使用不同的方法将类中的变量数减少为0

C# 如何使用不同的方法将类中的变量数减少为0,c#,class,object,methods,C#,Class,Object,Methods,我的问题是我使用了不同的方法,试图从一个最大值为15,最小值为-100的变量中减少一个int 我得到的结果 我想要的结果 我不知道我的逻辑是否错误,但我想到的是: 代码更新为“它工作^ u^” 使用系统; 职业玩家 { 私人弦乐演奏者姓名; 私人综合实力; 私人智能; 私人国际情报; 私有int_属性; 公共字符串名 { 收到 { 返回PlayerName; } 设置 { PlayerName=值; } } 公共综合实力 { 收到 { 回归力量; } 设置 { _强度=价值; } } 公共智

我的问题是我使用了不同的方法,试图从一个最大值为15,最小值为-100的变量中减少一个
int

我得到的结果

我想要的结果

我不知道我的逻辑是否错误,但我想到的是:

代码更新为“它工作^ u^”

使用系统;
职业玩家
{
私人弦乐演奏者姓名;
私人综合实力;
私人智能;
私人国际情报;
私有int_属性;
公共字符串名
{
收到
{
返回PlayerName;
}
设置
{
PlayerName=值;
}
}
公共综合实力
{
收到
{
回归力量;
}
设置
{
_强度=价值;
}
}
公共智能
{
收到
{
返回敏捷性;
}
设置
{
_敏捷=价值;
}
}
公共情报
{
收到
{
回归智能;
}
设置
{
_智力=价值;
}
}
公共int属性
{
收到
{
返回_属性;
}
设置
{
_属性=价值;
}
} 
GFG类
{
//公共静态int属性=15;
静态公共void Main()
{
var p=新玩家();
运动员(p);
强度(p);
敏捷性(p);
智力(p);
ShowReult(p);
} 
静态虚空玩家(玩家p)
{
Console.WriteLine(“冒险家的名字”);
p、 Name=Console.ReadLine();
p、 属性=15;
}
静态空洞强度(玩家p)
{
尝试
{
Console.WriteLine(“\n设置属性,每个属性最多5分。”+p.attributes);
Console.WriteLine(“输入您的力量:”);
p、 强度=Convert.ToInt32(Console.ReadLine());
如果(第5页)
{
而(p.强度>5)
{
Console.WriteLine(“[请选择较低的数字]”);
强度(p);
}
}
}捕获(例外情况除外){
Console.WriteLine(“[请输入数字]”);
强度(p);
}
}
静态虚空敏捷(玩家p)
{
尝试
{
Console.WriteLine(“\n设置您的敏捷性。每个人最多5点。您还有“+p.attributes+”点”);
WriteLine(“输入你的敏捷:”);
p、 Agility=Convert.ToInt32(Console.ReadLine());
如果(临5)
{
而(p.敏捷度>5)
{
Console.WriteLine(“[请选择较低的数字]”);
敏捷性(p);
}
}
}
捕获(例外情况除外)
{
Console.WriteLine(“[请输入数字]”);
敏捷性(p);
}
}
静态虚空智能(玩家p)
{
尝试
{
Console.WriteLine(“\n设置属性,每个属性最多5分。”+p.attributes);
控制台.WriteLine(“输入您的智能:”);
p、 Intelligence=Convert.ToInt32(Console.ReadLine());
如果(第5页)
{
而(p.智力>5)
{
Console.WriteLine(“[请选择较低的数字]”);
智力(p);
}
}
}
捕获(例外情况除外)
{
Console.WriteLine(“[请输入数字]”);
智力(p);
}
}
静态无效显示(玩家p)
{
Console.WriteLine(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
Console.WriteLine(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
Console.WriteLine(“冒险家的名字是:”+p.Name);
Console.WriteLine(“左点:+p.attributes”);
Console.WriteLine(“强度:”+(p.Strength));
Console.WriteLine(“敏捷:”+(p.Agility));
Console.WriteLine(“智能:+p.Intelligence”);
}
}
}

在场景中使用单个对象的一种简单方法是,在主方法中创建游戏玩家并将其传递给所有其他函数:

class GFG
{
    public static int attribures = 15;
    static public void Main()
    {
      var p = new GamePlayer();
      player(p);
      
      ...
    } 

    static void player(GamePlayer p)
    {
        Console.WriteLine("Name of the Adventurer");
        p.Name = Console.ReadLine();
        Console.WriteLine("Name of the Adventurer is: " + p.Name);
    }
}

这里有一些问题,但我想说你的主要问题是你在每个私有方法中创建了一个新的
玩家
,然后失去了对它的引用。我认为你是对的,实际上,我对这样做表示怀疑,但我不知道如何将单个对象用于不同的方法。有几个问题我认为我的逻辑是错误的?另一个主要问题是,您实际上没有减少属性值。您只需显示属性值15。你想1)显示他们给出的值2)更新值,如
attribures-=givenValue
3)在其中添加检查以确保他们给出的值不超过最大/当前值^^
class GFG
{
    public static int attribures = 15;
    static public void Main()
    {
      var p = new GamePlayer();
      player(p);
      
      ...
    } 

    static void player(GamePlayer p)
    {
        Console.WriteLine("Name of the Adventurer");
        p.Name = Console.ReadLine();
        Console.WriteLine("Name of the Adventurer is: " + p.Name);
    }
}