[英]C# Using other class method variables in Main
我嘗試制作一個簡單的基於文本的游戲,並且剛剛完成了在 Main 中編寫角色創建菜單的代碼。 現在我決定用單獨的 class Player 創建庫,並使用這段代碼創建 CharacterCreation 方法。 問題是 - 我不明白如何使 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;
好吧,我猜它可能是某種構造函數,但嘗試了幾個選項但它不起作用。 我的代碼看起來很笨拙,但我只學習了 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
}
}
我嘗試制作一個簡單的基於文本的游戲,並且剛剛完成在 Main 中編寫角色創建菜單的代碼。 現在我決定使用單獨的 class 播放器制作庫,並使用此代碼制作 CharacterCreation 方法。 問題是 - 我不明白如何讓 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;
好吧,我猜它可能是某種構造函數,但嘗試了幾個選項並且它不起作用。 我的代碼看起來很笨拙,但我只學習了一個月的 C# 所以不要嚴格判斷:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.