将一系列整数作为一组存储在一起并将它们与其他整数组进行比较的最佳方法是什么。我正在用C#编写一个RPG作为一个类项目并尝试编写自己的代码。我只比较3个Stats,一个Attack数据,一个防御数据和一个速度属性。我也有四个角色类。每个都有三个统计数据。我应该为字符类使用单独的类,还是字符串会更好地为我做这个?
答案 0 :(得分:1)
我只是在评论中敲定我所得到的内容。使用基础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;
}
}
始终尝试将代码保持干净
答案 1 :(得分:0)
我找到了自己更简单的方法。类的复杂性有点超出我的技能障碍(现在我将有一个编码类,它将正确地指导我们使用类)。我想出了我认为可能是我问题的最简单和最简单的答案。只需将统计数据用作播放器的静态整数,然后让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;
}