C# 如何将函数的返回值赋给变量?
也许这可以更清楚。该方法正在返回值…但在返回值后,我无法在程序的其余部分访问它。我对困难的部分发表了评论。C# 如何将函数的返回值赋给变量?,c#,C#,也许这可以更清楚。该方法正在返回值…但在返回值后,我无法在程序的其余部分访问它。我对困难的部分发表了评论。 class CharacterStats { public int CharacterHitPoints() { int Health = 0; Random rnd = new Random(); Health = rnd.Next(30,70); return (Health);
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();
}
}
}
注释就在上面的行中,这是我想要放置返回值的地方,并且可以访问程序的其余部分。
编辑:,因为您编辑的问题更清楚。我相信你正在寻找以下
您需要将返回值分配给代码中的某个变量,然后在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();
}
你只要打个电话就可以了
int strength = characterStatsObject.CharacterStrength()
创建CharacterStats对象后
CharacterStats objCS = new CharacterStats();
int CHP = objCS.CharacterHitPoints();
int CRS = objCS.CharacterStrength();
如果在MAIN子句中执行此操作,则可以使用这两个变量
var myVariable=Console.Readline()代码>??请遵循网络上的任何C#教程。我不是在阅读网络教程。我被课本上的方法第五章难住了。我的搜索结果是Steve在C、Python中找到了一些例子,但在C#中没有。无论如何,我将重新阅读我关于方法的一章。这在我看来相当复杂。好吧,那么我需要在函数中还是在我的主代码中创建对象呢?这不起作用。这将再次调用该函数。它没有将前一个方法的返回值赋给Main()中的变量。@MatthewOttewell,我不知道您是如何尝试的。但在您自己的代码中,您将从方法向变量赋值。参见第行Health=rnd.Next(30,70)代码>我编辑了我的文章,以显示程序的完整代码(除了另外两个与此问题无关的类)。这样做效果更好。它的工作并不完美,但我看到返回值在第一次返回时存储为变量。诚然,当我再次调用该函数时,它不会更新值。这没有道理……但这是一个很好的开始。我必须在之前而不是之后声明变量。我认为返回的值附加了一些我可以访问的内存。我想不是。
CharacterStats objCS = new CharacterStats();
int CHP = objCS.CharacterHitPoints();
int CRS = objCS.CharacterStrength();