-1

おそらく、これはもっと明確かもしれません。メソッドは値を返しています...しかし、値が返された後、プログラムの残りの部分で値にアクセスできません。難しい部分にコメントします。

class CharacterStats
{
    public int CharacterHitPoints()
    {
        int Health = 0;            
        Random rnd = new Random();
        Health = rnd.Next(30,70);
        return (Health);
    }

    public int CharacterStrength()
    {
        int Strength = 0;
        Random rnd = new Random();
        Strength = rnd.Next(1,20);
        return (Strength);
    }
}

class ApplicationObject
{
    static void Main()
    {
      string CharName;
      string KeepStats;

      MonsterAttackRolls GoblinAttack = new MonsterAttackRolls();
      MonsterAttackRolls OrcAttack = new MonsterAttackRolls();
      MonsterAttackRolls OgreAttack = new MonsterAttackRolls();
      MonsterAttackRolls DragonAttack = new MonsterAttackRolls();

      MonsterHitPoints GoblinHealth = new MonsterHitPoints();
      MonsterHitPoints OrcHealth = new MonsterHitPoints();
      MonsterHitPoints OgreHealth = new MonsterHitPoints();
      MonsterHitPoints DragonHealth = new MonsterHitPoints();

      CharacterStats PlayerHealth = new CharacterStats();
      CharacterStats PlayerStrength = new CharacterStats();

      Console.WriteLine("Welcome to Realm, the top adventure game in the world!  Please enter the name of your character if you wish to continue:");
      CharName = Console.ReadLine();

      Console.WriteLine("Welcome {0} to Realm!  You will be given stats.  They may be good or they may be bad.\nYou only get 2 rolls for your stats though, so pick wisely!  From there you must fight 4 fearsome beasts.\n  If you survive, you will win the game.  If you die, well, you die.  Press any key to continue.", CharName);
      Console.ReadLine();

      Console.WriteLine("You will be given a strength number which augments your damage. Your strength can range from 1 - 20.\n A strength of 15 or over gives you a bonus to damage.  A strength of 5 or less gives you a penalty.   You will be start with a hit point number between 30-70.  Good luck!");
      Console.ReadLine();

      Console.WriteLine("Your first set of stats is:\n Health = {0}.\nYour strength is {1}.\nIf you would like to roll again press 1.  If you want to keep your stats press any other key.", PlayerHealth.CharacterHitPoints(), PlayerStrength.CharacterStrength());
      KeepStats =  Console.ReadLine();

      if (KeepStats == "1")
      {
          Console.WriteLine("So you want a second set of stats.  Okay.\n Your health is now: {0}.\n Your strength is now: {1}.", PlayerHealth.CharacterHitPoints(), PlayerStrength.CharacterStrength());
          Console.ReadLine();
      }



        Console.WriteLine("Okay {0} you are ready to go.  Good luck!\n.  Once again, your stats are:  Health: {1}\n Strength: {2}", CharName, //How do I access the return value? x 2?;
        Console.ReadLine();
    }







    }
}

コメントがすぐ上の行にある場合は、戻り値を入れて、プログラムの残りの部分にアクセスできるようにします。

4

3 に答える 3

5

あなたは単に呼び出すことができます

int strength = characterStatsObject.CharacterStrength() 

CharacterStats-Objectを作成した後。

于 2012-10-23T06:58:42.450 に答える
3
CharacterStats charObj = new CharacterStats();
int CharacterStrengthInt = charObj.CharacterStrength();
int CharacterHitPointsInt = charObj.CharacterHitPoints();

編集:、あなたの編集された質問はそれをより明確にするので。私はあなたがフォローを探していると信じています。

コード内のいくつかの変数に戻り値を割り当ててから、それらの変数の値をConsole.WriteLineに表示する必要があります。そうしないと、それらは失われます。

次のようなもの。

int CharacterStrengthInt = PlayerStrength.CharacterStrength();
int CharacterHitPointsInt = PlayerHealth.CharacterHitPoints();
 if (KeepStats == "1")
      {
        Console.WriteLine("So you want a second set of stats.  Okay.\n Your health is now: {0}.\n Your strength is now: {1}."
       , CharacterHitPointsInt, CharacterStrengthInt);
          Console.ReadLine();
      }
于 2012-10-23T06:58:34.823 に答える
3
CharacterStats objCS = new CharacterStats();
int CHP = objCS.CharacterHitPoints();
int CRS = objCS.CharacterStrength();

MAIN句でこれを行うと、両方の変数を使用できます

于 2012-10-23T06:59:41.983 に答える