不确定是否使用字符串或类

本文关键字:字符串 是否 不确定 | 更新日期: 2023-09-27 18:20:57

将一系列整数存储为一组,并将它们与其他整数组进行比较的最佳方法是什么。我正在用C#编写一个RPG作为一个类项目,并试图编写自己的代码。我只比较了3个统计数据,一个攻击统计,一个防御统计和一个速度统计。我还有四个角色类。每一个都有三个统计数据。我应该为字符类使用单独的类吗?还是字符串会更好?

不确定是否使用字符串或类

我会在评论中详细说明我的意思。使用基本Character类:

public abstract class Character
{
    public int Attack { get; set; }
    public int Defense { get; set; }
    public int Speed { get; set; }
}

那么,您的类将继承Character。例如法师的防御能力很低:

public class Mage : Character
{
    public Mage()
    {
        Defense = 1;
        Attack = 5;
        Speed = 5; 
    }
}

始终尝试保持代码DRY

我找到了自己更简单的方法。类的复杂性有点超出了我的技能障碍(现在我将有一个编码类,它将正确地指导我们使用类)。我想出了一个我认为可能是最简单、最简单的答案来解决我的问题。只需将统计数据用作玩家的静态int,然后让readline使用While循环根据readline输入添加或减去统计数据。

        {
            int GoblinKingHealth = 100;
            int GoblinKingAtk = 7;
            int GoblinKingDef = 0;
            int GoblinKingSpd = 3;
            int PlayerHealth = 100;
            int PlayerAtk = 0;
            int PlayerDef = 0;
            int PlayerSpd = 0;
            Console.WriteLine( "Chose your class." );
            Console.WriteLine( "Fighter" );
            Console.WriteLine( "Ninja" );
            Console.WriteLine( "Mage" );
            Console.WriteLine( "Knight" );

            while( true )
            {
                string line = Console.ReadLine();
                if( line == "Fighter" )
                {
                    PlayerAtk = +4;
                    PlayerDef = +4;
                    PlayerSpd = +2;
                    Console.WriteLine( "You have chosen the Fighter class.  The Fighter class is the most well balanced class with average attack and defense and slightly under average speed" );
                    break;
                }
                if( line == "Ninja" )
                {
                    PlayerAtk = +5;
                    PlayerDef = +2;
                    PlayerSpd = +4;
                    Console.WriteLine("You have chosen the Ninja class.  The Ninja class is the fastest of the classes and has above average attack but a lower defense than the fighter.");
                    break;
                }
                if( line == "Mage" )
                {
                    PlayerAtk = +9;
                    PlayerDef = +0;
                    PlayerSpd = +3;
                    Console.WriteLine( "You have chosen the Mage class.  The Mage class has the strongest attack but the weakest defense and above average speed." );
                    break;
                }
                if( line == "Knight" )
                {
                    PlayerAtk = +1;
                    PlayerDef = +6;
                    PlayerSpd = +1;
                    Console.WriteLine( "You have chosen the Knight class.  The Knight class has near impenitrable defense at a heavy cost of speed and attack power." );
                    break;
                }