2012-09-17 34 views
1

大家都知道我剛剛開始編寫C#,這是實踐。從靜態void返回一個值到靜態void,使用公共變量 - 一些指導請

我在網上找到了一個GuessTheNumberGame的代碼,一直試圖改進基本的遊戲,以便給出反饋,並且用戶可以改變數字。 我有程序工作,但希望把'NumberChange'模塊與'代碼'方法分開。

我在我的'GuessTheNumberGame'內執行了一個Main()方法,執行'Code'方法。 事情是當我去改變數字範圍'NumberChange'模塊不會改變'公共靜態int從'或'公共靜態詮釋'的值;因爲這個數字的範圍保持不變。下面

代碼:

using System; 

namespace GuessThatNumber 
{ 
    class GuessTheNumberGame 
    { 
     static void Main() 
     { 
      Code(from, to, new_range); 
     } 

     public static int new_range = 0; 
     public static int from = 1; 
     public static int to = 10; 

     static void Code(int from, int to, int new_range) 
     { 
      //int from = 1; // The range the user will guess from. 
      //int to = 10; //The range the user will guess to. 


      int guessedNumber; //This will hold the value that the user guessed. 
      int Counter = 0; //Counter is used to count the number of guesses the user makes. 
      int selection = 0; //This value is for the selection at the start of the program 
      //int new_range = 0; 
      bool exit = false; 

      while (exit == false) 
      { 
       Console.WriteLine("What would you like to do?"); 
       Console.WriteLine("1: Alter the range of guessed numbers? The range is currently from {0} to {1}.", from, to); 
       Console.WriteLine("2: Try to guess the number?"); 
       Console.WriteLine("3: Exit the program?"); 
       Console.WriteLine("Please enter a number:"); 
       if (int.TryParse(Console.ReadLine(), out selection)) 
       { 
        if (selection == 2) 
        { 
         int randomNumber = new Random().Next(from, to); //Generates a random number between the (from, to) variables. 
         Console.Write("The number is between {0} and {1}. ", from, to); 
         while (true) 
         { 
          Console.Write("Make a guess: "); 
          if (int.TryParse(Console.ReadLine(), out guessedNumber)) 
          { 
           if (guessedNumber == randomNumber) 
           { 
            Console.WriteLine("You guessed the right number!"); 
            if (Counter < 2) 
            { 
             Console.WriteLine("You guessed the number in only 1 turn! Amazing!"); 
             Console.WriteLine(" "); 
            } 
            else 
            { 
             Console.WriteLine("You guessed " + Counter + " times."); 
             Console.WriteLine(" "); 
            } 
            break; 
           } 
           else 
           { 
            Console.WriteLine("Your guess was too {0}.", (guessedNumber > randomNumber) ? "high" : "low"); 
            Counter = Counter + 1; 
           } 
          } 
          else 
          { 
           Console.WriteLine("Input was not an integer."); 
          } 
         } 
         //Console.WriteLine(); 
         //Console.WriteLine("Press any key to exit."); 
         //Console.ReadKey(); 
        } 
        else if (selection == 1) 
        { 
         NumberChange(from, to, new_range); 
        } 

        else 
        { 
         exit = true; 
        } 
       } 
      } 
     } 

     static int NumberChange(int from, int to, int new_range) 
     { 
      Console.WriteLine("Please enter the number that the guess will start from."); 
      int.TryParse(Console.ReadLine(), out new_range); 
      from = new_range; 
      Console.WriteLine("Now enter the number that the guess will go to."); 
      int.TryParse(Console.ReadLine(), out new_range); 
      to = new_range; 
      return new_range; 
     } 
    } 
} 
+1

它不是一個 「空白」。這是一個**功能**。函數的返回類型是'void',但是將函數定義稱爲返回類型的實例是不正確的。 –

回答

-1

如果按引用傳遞的變量。然後通過他們的功能可以改變他們。

像這樣

static int NumberChange(ref int from, ref int to, ref int new_range) 
{ 
    //Your code here 
} 

ADN同樣爲您的代碼方法

+0

我爲什麼會陷入低谷? –

0

請閱讀有關passing parameters by reference in C#

到目前爲止,您的參數是按值傳遞的。這意味着,在你的程序的開始調用Code()public static int frompublic static int to當前值複製到參數fromto。當你調用NumberChange()

同樣的情況:參數(局部變量)的值fromto複製NumberChange()方法具有相同名稱的參數,但如果這些值內NumberChange,修改後的新的價值永遠不會從那裏回來;您剛剛修改了僅存在於相應方法內的局部變量fromto

相反,你可以使用ref關鍵字聲明的參數:

static int NumberChange(ref int from, ref int to, int new_range) 

這意味着,你將不得不使用ref關鍵字爲相應的參數來調用你的方法,太:

NumberChange(ref from, ref to, new_range); 

另外,請注意有關您的NumberChange()方法的兩個問題:

  • 您傳遞了new_range參數,但您不使用它。實際上,您在撥打TryParse()時會被覆蓋。因此,您可以簡單地將int new_range聲明爲局部變量,而不是將其作爲參數傳遞。
  • 您從該方法返回值,但不使用該值。因此,您可以將您的方法聲明爲void並刪除return語句。

最後,如果你希望你的public static int frompublic static int to變量由Code()改變,類似添加ref關鍵字NumberChange()。但是,對於您當前的代碼,這不是很有用,因爲程序在離開Code()後立即結束,並且根本不會使用新值。

+0

謝謝。這幫了很多忙。我不知道ref關鍵字。 此外代碼()使用NumberChange(),因爲它循環返回,直到退出爲True。所以如果用戶決定改變數字範圍,那麼這將保持有效,直到退出成爲真實。 – Inafune

+0

@Inafune:當然,但是數字範圍的變化完全在'Code()'內發生,並且與'public static int from'或'public static int to'無關。 如果答案已解決您的問題,請將答案標記爲已接受。 –

0

您正在使用靜態變量,然後將它們傳遞給已有權訪問它們的方法。傳遞它們基本上覆蓋了本地靜態版本。

你的方法改成這樣:

private static void NumberChange() 
    { 
     int new_range; 

     Console.WriteLine("Please enter the number that the guess will start from."); 
     int.TryParse(Console.ReadLine(), out new_range); 
     from = new_range; 
     Console.WriteLine("Now enter the number that the guess will go to."); 
     int.TryParse(Console.ReadLine(), out new_range); 
     to = new_range; 
    } 


    static void Code() 
    { 


    } 

,並刪除這一行:

public static int new_range = 0; 
+0

只要不對'Code()'方法做同樣的事情,這也不會改變任何東西。 –

+0

謝謝,錯過了那一個。 –