[英]C# Using other class method variables in Main
I try to make a simple text based game and just finished to write a code of character creation menu in Main.我尝试制作一个简单的基于文本的游戏,并且刚刚完成了在 Main 中编写角色创建菜单的代码。 Now i decide to make library with separate class Player and make the CharacterCreation method there with this code.现在我决定用单独的 class Player 创建库,并使用这段代码创建 CharacterCreation 方法。 The problem is - i dont understand how to make Player.CharCreation method variables(like player name and attributes) now visible in Main programm to use them after.问题是 - 我不明白如何使 Player.CharCreation 方法变量(如玩家名称和属性)现在在主程序中可见以便之后使用它们。
public class Player
{
public static void CharCreation()
{
Console.WriteLine("\t=== Character creation menu ===\n");
Console.Write("Write the name and press Enter: ");
string name = Console.ReadLine();
string Again;
string RaceChoise;
Console.Write("\nChoose your race(print number and press Enter):\n 1.Human\n 2.Dwarf\n 3.Elf\n");
// Race choise
do
{
Again = "No";
RaceChoise = Console.ReadLine();
switch (RaceChoise)
{
case "1":
Console.WriteLine("\nYou was born as human (Charisma +1)\n");
RaceChoise = "Human";
break;
case "2":
Console.WriteLine("\nYou was born as dwarf(Constitution +1)\n");
RaceChoise = "Dwarf";
break;
case "3":
Console.WriteLine("\nYou was born as elf (Agility +1)\n");
RaceChoise = "Elf";
break;
default:
Console.WriteLine("Wrong number");
Again = RaceChoise;
break;
}
} while (Again != "No");
// Base attributes according to the race choise
Console.WriteLine("Character attributes\n");
int Str = 1;
int Agi = 1;
int Int = 1;
int Con = 1;
int Cha = 1;
Agi = RaceChoise == "Elf" ? (Agi + 1) : (Agi);
Con = RaceChoise == "Dwarf" ? (Con + 1) : (Con);
Cha = RaceChoise == "Human" ? (Cha + 1) : (Cha);
Console.Write($"Strengh: {Str}\nAgility: {Agi}\nIntelligence: {Int}\nConstitution: {Con}\nCharisma: {Cha}\n\n");
Console.WriteLine("Above is your character base attributes. Now distribute 5 points more");
// Attributes points destribution
int Counter = 5;
for (int i = 0; i < 1; i++)
{
Console.Write("To strengh: ");
int StrChange = Convert.ToInt32(Console.ReadLine());
if (StrChange > Counter) { StrChange = Counter; }
Counter = Counter - StrChange;
Str = Str + StrChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To agility: ");
int AgiChange = Convert.ToInt32(Console.ReadLine());
if (AgiChange > Counter) { AgiChange = Counter; }
Counter = Counter - AgiChange;
Agi = Agi + AgiChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To intelligence: ");
int IntChange = Convert.ToInt32(Console.ReadLine());
if (IntChange > Counter) { IntChange = Counter; }
Counter = Counter - IntChange;
Int = Int + IntChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To constitution: ");
int ConChange = Convert.ToInt32(Console.ReadLine());
if (ConChange > Counter) { ConChange = Counter; }
Counter = Counter - ConChange;
Con = Con + ConChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To charisma: ");
int ChaChange = Convert.ToInt32(Console.ReadLine());
if (ChaChange > Counter) { ChaChange = Counter; }
Cha = Cha + ChaChange;
Well i guess its might be some kind of constructor but tryed a several options and its not working.好吧,我猜它可能是某种构造函数,但尝试了几个选项但它不起作用。 My code looks like clumsy but i learning C# for a month only so dont judge strictly:)我的代码看起来很笨拙,但我只学习了 C# 一个月所以不要严格判断:)
public class Player
{
public string Name { get; set; }
public string Race { get; set; }
public static Player CharCreation()
{
// do your stuff...
string name = "Player";
string race = "Human";
return new Player()
{
Name = name,
Race = race,
};
}
}
class Program
{
static void Main()
{
Player player = Player.CharCreation();
// do something with player.Name or player.Race
}
}
I try to make a simple text based game and just finished to write a code of character creation menu in Main.我尝试制作一个简单的基于文本的游戏,并且刚刚完成在 Main 中编写角色创建菜单的代码。 Now i decide to make library with separate class Player and make the CharacterCreation method there with this code.现在我决定使用单独的 class 播放器制作库,并使用此代码制作 CharacterCreation 方法。 The problem is - i dont understand how to make Player.CharCreation method variables(like player name and attributes) now visible in Main programm to use them after.问题是 - 我不明白如何让 Player.CharCreation 方法变量(如播放器名称和属性)现在在主程序中可见以便之后使用它们。
public class Player
{
public static void CharCreation()
{
Console.WriteLine("\t=== Character creation menu ===\n");
Console.Write("Write the name and press Enter: ");
string name = Console.ReadLine();
string Again;
string RaceChoise;
Console.Write("\nChoose your race(print number and press Enter):\n 1.Human\n 2.Dwarf\n 3.Elf\n");
// Race choise
do
{
Again = "No";
RaceChoise = Console.ReadLine();
switch (RaceChoise)
{
case "1":
Console.WriteLine("\nYou was born as human (Charisma +1)\n");
RaceChoise = "Human";
break;
case "2":
Console.WriteLine("\nYou was born as dwarf(Constitution +1)\n");
RaceChoise = "Dwarf";
break;
case "3":
Console.WriteLine("\nYou was born as elf (Agility +1)\n");
RaceChoise = "Elf";
break;
default:
Console.WriteLine("Wrong number");
Again = RaceChoise;
break;
}
} while (Again != "No");
// Base attributes according to the race choise
Console.WriteLine("Character attributes\n");
int Str = 1;
int Agi = 1;
int Int = 1;
int Con = 1;
int Cha = 1;
Agi = RaceChoise == "Elf" ? (Agi + 1) : (Agi);
Con = RaceChoise == "Dwarf" ? (Con + 1) : (Con);
Cha = RaceChoise == "Human" ? (Cha + 1) : (Cha);
Console.Write($"Strengh: {Str}\nAgility: {Agi}\nIntelligence: {Int}\nConstitution: {Con}\nCharisma: {Cha}\n\n");
Console.WriteLine("Above is your character base attributes. Now distribute 5 points more");
// Attributes points destribution
int Counter = 5;
for (int i = 0; i < 1; i++)
{
Console.Write("To strengh: ");
int StrChange = Convert.ToInt32(Console.ReadLine());
if (StrChange > Counter) { StrChange = Counter; }
Counter = Counter - StrChange;
Str = Str + StrChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To agility: ");
int AgiChange = Convert.ToInt32(Console.ReadLine());
if (AgiChange > Counter) { AgiChange = Counter; }
Counter = Counter - AgiChange;
Agi = Agi + AgiChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To intelligence: ");
int IntChange = Convert.ToInt32(Console.ReadLine());
if (IntChange > Counter) { IntChange = Counter; }
Counter = Counter - IntChange;
Int = Int + IntChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To constitution: ");
int ConChange = Convert.ToInt32(Console.ReadLine());
if (ConChange > Counter) { ConChange = Counter; }
Counter = Counter - ConChange;
Con = Con + ConChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To charisma: ");
int ChaChange = Convert.ToInt32(Console.ReadLine());
if (ChaChange > Counter) { ChaChange = Counter; }
Cha = Cha + ChaChange;
Well i guess its might be some kind of constructor but tryed a several options and its not working.好吧,我猜它可能是某种构造函数,但尝试了几个选项并且它不起作用。 My code looks like clumsy but i learning C# for a month only so dont judge strictly:)我的代码看起来很笨拙,但我只学习了一个月的 C# 所以不要严格判断:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.