2016-04-04 73 views
0

所以我是一個新鮮的初學者,我做了這個小遊戲,敵人在0hp死亡,但我的程序在第一次使用損害能力C#如何讓我的代碼返回到起點(初學者)

static void Main(string[] args) 
     { 
      string hp = ""; 
      int damage; 
      int level; 
      int experience = 0; 
      int kills = 0; 
      int enemy1hp = 200; 


      level = 1 + experience; 
      experience = kills * 1; 
      damage = 50 * level; 
      enemy1hp = 200; 

      Console.WriteLine("Welcome!"); 
      hp = "Enemy 1 currently has " + enemy1hp.ToString() + "hp!"; 
      Console.WriteLine(hp); 

      string userValue = Console.ReadLine(); 
      if (userValue == "1") 
      { 
       Console.WriteLine("You have used Haven Strike!"); 
       enemy1hp = enemy1hp - damage; 
       hp = "Enemy 1 currently has " + enemy1hp.ToString() + "hp!"; 
      } 

      Console.WriteLine(hp); 
      Console.ReadLine(); 

      if (enemy1hp <= 0) 
      { 
       kills = kills + 1; 
      } 
     } 

任何想法我可以改變?

回答

3

使用while循環這個

static void Main(string[] args) 
{ 
    string hp = ""; 
    int damage; 
    int level; 
    int experience = 0; 
    int kills = 0; 
    int enemy1hp = 200; 

    while (enemy1hp > 0) 
    { 
     level = 1 + experience; 
     experience = kills * 1; 
     damage = 50 * level; 

     Console.WriteLine("Welcome!"); 
     hp = "Enemy 1 currently has " + enemy1hp.ToString() + "hp!"; 
     Console.WriteLine(hp); 

     string userValue = Console.ReadLine(); 
     if (userValue.equals("1")) 
     { 
      Console.WriteLine("You have used Haven Strike!"); 
      enemy1hp = enemy1hp - damage; 
      hp = "Enemy 1 currently has " + enemy1hp.ToString() + "hp!"; 
     } 

     Console.WriteLine(hp); 
     Console.ReadLine(); 

     if (enemy1hp <= 0) 
     { 
      kills = kills + 1; 
     } 
    } 
} 

這隻要enemy1hp是運行速度比0大你也應該刪除enemy1hp = 200;分配。

+1

最重要的是,不要這樣做的主要。閱讀關於類屬性和方法。 –

+0

你確定你想從程序的開始循環?每次都重置變量? –

+0

即使代碼正常工作,這也不是一個好的答案。主要做什麼?如果他是初學者,那麼現在就應該教他一些好習慣。這就像對有人抱怨生命如此艱難:扼殺自己。 最簡單的答案,但不是最好的答案。 –