我試圖在主類中設置一個字符串,並將其發送給我的名稱類,然後稍後在主類的另一個方法中使用它。 這就是我所擁有的。如何在一個類中創建一個字符串並在另一個類中使用它
我對編碼相當陌生,今年剛上課。任何幫助。
我只是想做一個簡單的冒險遊戲。
提前致謝!
class Player
{
public string playerName { get; set; }
}
class MainClass
{
public static void nameSelection()
{
Player player = new Player();
Console.Clear();
Console.WriteLine ("Let's start off simple adventurer, what is your name?");
player.playerName = Console.ReadLine();
Console.Clear();
Console.WriteLine ("Are you sure {0} is your name? Type 1 for yes and 2 for no", player.playerName);
string confirm = Console.ReadLine();
if (confirm == "1") {
Console.Clear();
Console.WriteLine ("Okay {0}, here we go!", player.playerName);
Console.ReadLine();
}
else if (confirm == "2") {
Console.Clear();
nameSelection();
}
else
{
Console.Clear();
nameSelection();
}
}
public static void classselsction()
{
Console.ReadLine();
Console.WriteLine ("As you get to the end of the hallway you see a shadow.");
Console.ReadLine();
Console.WriteLine("Hello {0}, I see you have managed to escape from your cell. " +
"You have proven yourself quite worthey.", player.playerName);
Console.ReadLine();
}
}
嗨,歡迎來到SO。您是否收到錯誤,您得到的結果與您的期望不符? – miltonb