2013-03-08 39 views
0

新的C#er中跳過一行方法到一行。我正在製作一個控制檯RPG。我創建了一個保存和加載方法,它將變量寫入一個txt文件,並在某個時間點擊「保存遊戲」或「加載遊戲」時加載它們。我試圖讓LoadGame()方法跳轉到我的Main()中的一個點。我意識到我可以重寫一些我的代碼並使用一些if語句,或者轉到並且只是拋棄我的LoadGame(),但我更願意將我的LoadGame()保留在Main()之外。C#控制檯應用程序,在主方法

這基本上是我的代碼是什麼樣子:

public static void LoadGame() 

    { 
     TextReader tr = new StreamReader("SavedGame.txt"); 

     string charnameload = tr.ReadLine(); 
     string hpload = tr.ReadLine(); 
     string hpmaxload = tr.ReadLine(); 
     string charattackload = tr.ReadLine(); 
     string charmanaload = tr.ReadLine(); 
     string charmanamaxload = tr.ReadLine(); 
     string charmanapowerload = tr.ReadLine(); 
     string spellsknownload = tr.ReadLine(); 
     string holyblastknownload = tr.ReadLine(); 
     string greaterhealknownload = tr.ReadLine(); 
     string goldload = tr.ReadLine(); 
     string expload = tr.ReadLine(); 
     string levelload = tr.ReadLine(); 
     string inventoryWeaponload = tr.ReadLine(); 
     string inventoryArmorload = tr.ReadLine(); 
     string inventoryshieldload = tr.ReadLine(); 
     string inventoryPotion1load = tr.ReadLine(); 
     string inventoryPotion2load = tr.ReadLine(); 
     string inventoryPotion3load = tr.ReadLine(); 
     string inventoryKey1load = tr.ReadLine(); 
     string inventoryKey2load = tr.ReadLine(); 
     string inventoryKey3load = tr.ReadLine(); 
     string inventoryKey4load = tr.ReadLine(); 
     string inventoryKey5load = tr.ReadLine(); 

     characterName = Convert.ToString(charnameload); 
     hp = Convert.ToInt32(hpload); 
     hpmax = Convert.ToInt32(hpmaxload); 
     charattack = Convert.ToInt32(charattackload); 
     charmana = Convert.ToInt32(charmanaload); 
     charmanamax = Convert.ToInt32(charmanamaxload); 
     charmanapower = Convert.ToInt32(charmanapowerload); 
     spellsknown = Convert.ToInt32(spellsknownload); 
     holyblastknown = Convert.ToInt32(holyblastknownload); 
     greaterhealknown = Convert.ToInt32(greaterhealknownload); 
     gold = Convert.ToInt32(goldload); 
     exp = Convert.ToInt32(expload); 
     level = Convert.ToInt32(levelload); 
     inventoryWeapon = Convert.ToString(inventoryWeaponload); 
     inventoryArmor = Convert.ToString(inventoryArmorload); 
     inventoryshield = Convert.ToString(inventoryshieldload); 
     inventoryPotion1 = Convert.ToString(inventoryPotion1load); 
     inventoryPotion2 = Convert.ToString(inventoryPotion2load); 
     inventoryPotion3 = Convert.ToString(inventoryPotion3load); 
     inventoryKey1 = Convert.ToString(inventoryKey1load); 
     inventoryKey2 = Convert.ToString(inventoryKey2load); 
     inventoryKey3 = Convert.ToString(inventoryKey3load); 
     inventoryKey4 = Convert.ToString(inventoryKey4load); 
     inventoryKey5 = Convert.ToString(inventoryKey5load); 

     tr.Close(); 
     CheckInventory(); 
     CheckSpell(); 

    //need statement here to skip to a point in Main() 

    } 

static void Main() 
{ 
    //skip to somewhere in here 
} 

回答

1

你回想起前面。您可以在主要方法中調用您希望執行的方法

例如,

static void Main() 
{ 
    LoadGame(); 
} 
+0

是的,我明白這一點。這不是問題。當LoadGame()方法發生時,有一些東西需要跳過。也許我沒有足夠好地解釋我的問題。我已經在Main()中調用了LoadGame()方法。我有一個switch = case.ReadLine();在這種情況下(「加載遊戲」)執行LoadGame()方法。 我需要的方法跳到Main()中的一行代碼。 – 2013-03-08 13:07:22

+1

將您的LoadGame或其他代碼分成多個方法,執行您需要的特定任務,然後根據需要調用或不調用它們。然後,您可以從您的方法中返回值以決定要執行的操作。 – 2013-03-08 13:09:28

+0

我想通了。我只需要拋出一些if語句。我不需要添加任何東西到我的LoadGame()方法。感謝您的幫助! – 2013-03-08 13:21:02