2011-08-04 172 views
0

我有一個while循環,直到bool done = true; 在TestMoves()方法中,根據用戶輸入,該方法返回布爾值爲true或false。然而,我不知道如何「發送」這個值回到我的Start()方法中的while循環來停止循環。這裏是我的代碼:如何從方法返回bool值

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 


     public static void Main(string[] args) 
     { 
      Start("r"); 
     } 

     public static string Start(string move) 
     { 

      Console.Write("Welcome to the Shotgun App\nEnter s for single player and m for multiplayer: "); 
      string gameType = Console.ReadLine(); 

      if (gameType == "s") 
      { 

       Console.Write("Single Player Controls:\n r = reload\n s = shield\n f = fire\n ***you start with ammo\n Ready to play?"); 
       Console.ReadLine(); 

       int ammo = 1; 

       bool done = false; 
       while (!done) 
       { 
        Console.Write("\nEnter your move: "); 
        move = Console.ReadLine(); 


        switch (move) 
        { 
         case "r": 
          Console.Write("\nYou have reloaded, press enter for Genius\n"); 

          ammo++; 
          Console.Write("Your ammo is " + ammo); 

          Console.ReadLine(); 

          string geniusMove = ""; 
          Genius(geniusMove, move, done); 
          Console.ReadLine(); 




          break; 
         case "s": 
          Console.Write("\nYou have shielded, press enter for Genius\n"); 

          Console.Write("Your ammo is " + ammo); 

          Console.ReadLine(); 

          geniusMove = ""; 
          Genius(geniusMove, move, done); 
          Console.ReadLine(); 




          break; 
         case "f": 
          if (ammo != 0) 
          { 
           Console.Write("\nYou have fired, press enter for Genius\n"); 

           ammo--; 
           Console.Write("Your ammo is " + ammo); 

           Console.ReadLine(); 

           geniusMove = ""; 
           Genius(geniusMove, move, done); 
           Console.ReadLine(); 
          } 
          else 
          { 
           Console.Write("You don't have enough ammo, try again"); 
           done = false; 
          } 
          break; 
         default: 
          Console.Write("\nInvalid move, try again\n"); 
          done = false; 
          break; 
        } 


       } 
       return move; 
      } 
      else 
      { 
       return move; 
      } 
     } 

     static string Genius(string geniusMove, string move, bool done) 
     { 
      int geniusAmmo = 1; 

      geniusMove = "r"; 
      if (geniusMove == "f") 
      { 

       geniusAmmo--; 
       Console.Write("Genius had decided to fire.\nGenius ammo is " + geniusAmmo + "\n"); 
      } 
      else if (geniusMove == "r") 
      { 

       geniusAmmo++; 
       Console.Write("Genius had decided to reload.\nGenius ammo is " + geniusAmmo + "\n"); 
      } 
      else if (geniusMove == "s") 
      { 
       Console.Write("Genius had decided to shield.\nGenius ammo is " + geniusAmmo + "\n"); 
      } 
      TestMoves(move, geniusMove, done); 
      return geniusMove; 
     } 


     static bool TestMoves(string move, string geniusMove, bool done) 
     { 

      if (move == "s" && geniusMove == "f") 
      { 
       Console.Write("No one has died yet"); 
       done = false; 
       return done; 
      } 
      else if (move == "f" && geniusMove == "f") 
      { 
       Console.Write("You both died! Good game!"); 
       done = true; 
       return done; 
      } 
      else if (move != "s" && geniusMove == "f") 
      { 
       Console.Write("You died! Good game!"); 
       done = true; 
       return done; 
      } 
      else if (move == "f" && geniusMove == "s") 
      { 
       Console.Write("No one has died yet"); 
       done = false; 
       return done; 
      } 
      else if (move == "f" && geniusMove != "s") 
      { 
       Console.Write("Genius died! Good game!"); 
       done = true; 
       return done; 
      } 
      else if (move != "f" && geniusMove != "f") 
      { 
       Console.Write("No one has died yet"); 
       done = false; 
       return done; 
      } 
      else 
      { 
       return done; 
      } 

     } 
    } 
} 
+3

你爲什麼要到一個指定的值變量,只是爲了回報它?只需在TestMoves中使用'return false'或'return true' ... –

回答

2

是否有需要致電的原因TestMovesGenius內部而不是從你的循環?在我看來,你的代碼可以像這樣重寫:

//Every instance of: 

string geniusMove = ""; 
Genius(geniusMove, move, done); 
Console.ReadLine(); 

//seems like it could be rewritten as: 

string geniusMove = ""; 
Genius(geniusMove, move, done); 
done = TestMoves(geniusMove, move, done); 
Console.ReadLine(); 
//and then you can remove the call to TestMoves from Genius 

所有代碼的整體流程有點讓我感到困惑。你有每個函數返回一個值,但似乎沒有做任何與返回值。我有一種感覺,通過一些重構,你可以使這個代碼更短更合乎邏輯。

看你的代碼後多一點的樣子,你可以撥打電話到TestMoves在你的循環的末尾:

    default: 
         Console.Write("\nInvalid move, try again\n"); 
         done = false; 
         break; 
       } 

       //add it here: 
       done = TestMoves(geniusMove, move, done); 

      } 
      return move; 
+0

非常感謝,該組織更符合邏輯 –

0

您可以通過參考傳遞值:

static string Genius(string geniusMove, string move, ref bool done) ... 

而且從TestMoves返回它:

static bool TestMoves(string move, string geniusMove) ... 

要叫它:

Genius(geniusMove, move, ref done); 
0

由於bool是值類型,不是引用類型,你不能像這樣通過布爾。使用out關鍵字來明確指定你想要把它作爲一個參考:

http://msdn.microsoft.com/en-us/library/ee332485.aspx

+0

bool不一定是作爲參考傳遞的引用類型。靜態無效TestRef(裁判bool b){b =!b;}編譯就好...並做我期望它,當我調用它。 out和ref之間的區別在於,在調用函數之前,必須初始化由ref傳遞的參數,而out傳遞的值必須在被調用的方法內初始化。 – Vladislav

+0

@Vladislav Hubrid在他的方法參數中沒有* ref *。無論他使用'out'還是'ref',只要按照他的示例代碼中的值傳遞它都是行不通的。 –

+0

我很抱歉 - 我正在閱讀關於ref vs vs的討論。這是我的錯誤 - 在問題的背景下,你的回答是完全正確的。 – Vladislav

相關問題