2017-01-08 1 views
1

私は、キャラクタを作成したり、キャラクタを修正したり、武器を装備したり、ユーザがキャラクタデータを入力したりできるコンソールアプリケーションを作成しています。私の最初の質問です。私はどのように私のユーザーのエントリをキャプチャし、それらの値をコンストラクタに渡すのですか?私は文字クラスを作成して、私のコンストラクタ変数も作成しました。私はキャラクタークラスにゲッターとセッターも含めました。追加するには、どのように私はこのキャラクターに武器を装備するつもりですか?コンストラクタクラスにデータを渡す

static void CreateCharacter() 
     { 
     //Declare my variables 
     string charName; 
     int charBaseAttack; 
     int charHealth; 
     int charAge; 
     int charSaiyanLevel; 



     //Ask for user input 
     Console.Write("Please enter the name of your character"); 
     charName = Console.ReadLine(); 

     Console.Write("Thanks for that, now enter a Base Attack level please: "); 
     charBaseAttack = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Thanks for that, now enter a Health level please: "); 
     charHealth = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Thanks for that, now how old is your character: "); 
     charAge = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Thanks for that, his or her Super Saiyan level please: "); 
     charSaiyanLevel = Convert.ToInt32(Console.ReadLine()); 



     //Instantiate my person 
     Character userCharacter = new Character(charName, charBaseAttack, charHealth, charAge, charSaiyanLevel); 

//ので、あなたの代わりにゲッター/セッターのプロパティを使用するには、お使いのプレーヤーのクラスを変更することができれば、私は、あなたのコードは、C#であることを仮定してい

private string mName; 
    private int mBaseAttack; 
    private int mHealth; 
    private int mAge; 
    private int mSaiyanLevel; 

    public Character(string _mName, int _mBaseAttack, int _mHealth, int _mAge, int _mSaiyanLevel) 
    { 
     //Initializing my member varaibles 
     mName = _mName; 
     mBaseAttack = _mBaseAttack; 
     mHealth = _mHealth; 
     mAge = _mAge; 
     mSaiyanLevel = _mSaiyanLevel; 
    } 

    public Character() 
    { 
     Character userCharacter = new Character(); 
    } 




    public string getName() 
    { 
     return mName; 

    } 
    public int getBaseAttack() 
    { 
     return mBaseAttack; 

    } 
    public int getHealth() 
    { 
     return mHealth; 

    } 
    public int getAge() 
    { 
     return mAge; 

    } 

    public int getSaiyanLevel() 
    { 
     return mSaiyanLevel; 

    } 

    public void setName(string _mName) 
    { 
     mName = _mName; 


    } 

    public void setBaseAttack(int _mBaseAttack) 
    { 
     mBaseAttack = _mBaseAttack; 


    } 

    public void setHealth(int _mHealth) 
    { 

     mHealth = _mHealth; 

    } 

    public void setAge(int _mAge) 
    { 

     mAge = _mAge; 

    } 

    public void setSaiyanLevel(int _SaiyanLevel) 
    { 

     mSaiyanLevel = _SaiyanLevel; 
+0

CreateCharacter()を実行するとどうなりますか?ユーザーに入力を促しますか? – StaticBeagle

+0

はい、それは正しいです。文字属性の入力をユーザーに促します。 – JGreen5278

+0

「あなたのユーザーのエントリをキャプチャし、それらの値をコンストラクタに渡すにはどうすればいいですか」というのはどういう意味ですか? CreateCharacterメソッドはCharacterを正しく作成しているようです。 – StaticBeagle

答えて

0

私の文字クラス。プロパティにゲッター/セッターから変更すると、あなたがあなたのコードに次の変更を加えることができ、文字を作成するために、あなたのプログラムのクラスを取得するには

public class Character 
{ 
    public string Name { get; set; } 
    public int BaseAttack { get; set; } 
    public int Health { get; set; } 
    public int Age { get; set; } 
    public int SaiyanLevel { get; set; } 

    public Character(string _mName, int _mBaseAttack, int _mHealth, int _mAge, int _mSaiyanLevel) 
    { 
     //Initializing my member varaibles 
     Name = _mName; 
     BaseAttack = _mBaseAttack; 
     Health = _mHealth; 
     Age = _mAge; 
     SaiyanLevel = _mSaiyanLevel; 
    } 

    // The default constructor is just initializing a local variable userCharacter 
    // to a new Character() that will be destroyed once it goes out of scope. 
    // If you need some default initialization 
    // you could do: 
    public Character() : this(string.Empty, -1, -1, -1, -1) { } 
    // I've initialized all int fields to -1 since it's a value 
    // that none of these fields (hopefully) should ever have 
    // Original default constructor 
    //public Character() 
    //{ 
    // Character userCharacter = new Character(); 
    // Once the code reaches the } below, userCharacter will 
    // be destroyed 
    //} 

    // Overrode the ToString method so that you can print 
    // the characters to the console 
    public override string ToString() 
    { 
     return string.Concat("Character Name: ", Name, " Base Attack: ", BaseAttack, " Health: ", Health, " Age: ", Age, " Saiyan Level: ", SaiyanLevel); 
    } 
} 

を必要だものではありません。私はコードスニペットでできる限り説明しようとしましたが、質問があれば自由にコメントしてください。

class Program 
{ 
    // If you want to use CreateCharacter() to return a newly created character, 
    // You could have CreateCharacter() return a Character 
    public static Character CreateCharacter() 
    { 
     //Declare my variables 
     string charName; 
     int charBaseAttack; 
     int charHealth; 
     int charAge; 
     int charSaiyanLevel; 

     //Ask for user input 
     Console.Write("Please enter the name of your character: "); 
     charName = Console.ReadLine(); 

     Console.Write("Thanks for that, now enter a Base Attack level please: "); 
     charBaseAttack = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Thanks for that, now enter a Health level please: "); 
     charHealth = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Thanks for that, now how old is your character: "); 
     charAge = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Thanks for that, his or her Super Saiyan level please: "); 
     charSaiyanLevel = Convert.ToInt32(Console.ReadLine()); 

     //Instantiate my person 
     return new Character(charName, charBaseAttack, charHealth, charAge, charSaiyanLevel); 
    } 

    public static void Main(string[] Args) 
    { 
     // Two ways to instantiate a Character 
     // 1. Change the return type of CreateCharacter() to return a Character object instead of void 
     // 2. Copy & past the contents of CreateCharacter() into main 
     //Declare my variables 
     string charName; 
     int charBaseAttack; 
     int charHealth; 
     int charAge; 
     int charSaiyanLevel; 

     //Ask for user input 
     Console.Write("Please enter the name of your character: "); 
     charName = Console.ReadLine(); 

     Console.Write("Thanks for that, now enter a Base Attack level please: "); 
     charBaseAttack = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Thanks for that, now enter a Health level please: "); 
     charHealth = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Thanks for that, now how old is your character: "); 
     charAge = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Thanks for that, his or her Super Saiyan level please: "); 
     charSaiyanLevel = Convert.ToInt32(Console.ReadLine()); 

     //Instantiate my person 
     Character userCharacter1 = new Character(charName, charBaseAttack, charHealth, charAge, charSaiyanLevel); 
     System.Console.WriteLine(); 
     Character userCharacter2 = CreateCharacter(); 

     // Print both characters to the console 
     System.Console.WriteLine(); 
     System.Console.WriteLine("First character stats:"); 
     System.Console.WriteLine(userCharacter1.ToString()); 
     System.Console.WriteLine(); 
     System.Console.WriteLine("Second character stats:"); 
     System.Console.WriteLine(userCharacter2.ToString()); 
    } 
} 
関連する問題