2016-02-15 85 views
0

首先,我想說我仍然是C#的初學者,所以向我解釋信息時,請不要使用我不會理解的複雜術語。第二,我完成了大部分工作,而且我並沒有要求其他人完成我的工作,我只是尋求幫助,因爲我不明白什麼是錯的/爲什麼它不工作。 三,我的程序不完整,除非我的隨機發生器正在工作,否則我無法完成它。 所以他這樣說,是我遇到的問題是,當我嘗試運行該程序時,系統強調單詞「隨機」在我的代碼開始,並說隨機數發生器問題

「A字段初始不能引用非靜態字段,方法或 屬性「。

爲什麼這樣做?如果我將這兩行代碼放在「Public Guess()」部分中,那麼編譯器運行良好,它只是表示我的「if」語句不工作,因爲容器「random」不存在。我不知道我還能做什麼,我真的會非常感謝您的幫助。 我的代碼如下:

public partial class Guess : Form 
{ 
    /*This is a "Guess the number" program. Then this program is run, 
    * I want to create two containers for the "TryParse" portion of this program 
    and then I want a number to be randomly generated for the user to guess, then 
    I want one last container to count how many guess it took the user.*/ 

    string number; 
    int guess; 
    Random random; 
    int randomnumber; 
    int counter; 


    public Guess() 
    { 
     /*Once the program is initalized, I want the 2nd button hidden until the first one 
     is clicked with a value in the textbox*/ 
     InitializeComponent(); 
     btnexe2.Hide(); 
     random = new Random(); 
     randomnumber = random.Next(0, 101); 

    } 
    private void btnClose_Click(object sender, EventArgs e) 
    { 
     //This closes the program// 
     Close(); 
    } 
    private void btnexe1_Click(object sender, EventArgs e) 
    { 
     /*This is where I will be doing most of my variable checking. First, 
     I want to check if the user left the textbox empty, if it is then 
     display a message box saying to enter a number.*/ 
     if (string.IsNullOrEmpty(tbnumber.Text)) 
     { 
      MessageBox.Show("Please enter a number from 0-100."); 
     } 
     else 
     {/*If it is not empty, then I want the system to determine if the variable 
     that has been entered can be converted to a int.*/ 
      number = Convert.ToString(tbnumber.Text); 
      if (Int32.TryParse(number, out guess)) 
      { 
       /*If the value can be converted, then i want the system to see if 
     it is lower, higher, or equal to the random value. Then I want the fist button hidden, 
     and the second one shown. Then I want to record how many times the user guessed.*/ 
       if (guess < randomnumber) 
       { 
        btnexe1.Hide(); 
        btnexe2.Show(); 
        this.BackColor = System.Drawing.Color.LightSeaGreen; 
        lbloutput.Text = "Too Low"; 
        counter=counter + 1; 
       } 
       else if (guess > randomnumber) 
       { 
        btnexe1.Hide(); 
        btnexe2.Show(); 
        this.BackColor = System.Drawing.Color.SlateBlue; 
        lbloutput.Text = "Too High"; 
        counter = counter + 1; 
       } 
       else 
       { 
        lbloutput.Text = "Good Guess"; 
        counter = counter + 1; 
       } 
      } 
      else 
      { 
       /*If the value cannot be converted to a int, then display a message box saying so.*/ 
       MessageBox.Show("This is not a number. Please enter a number between 0-100.");      
      } 
     } 
    } 
    private void btnexe2_Click(object sender, EventArgs e) 
    {/*I want to check if the user left the textbox empty, if it is then 
     display a message box saying to enter a number.*/ 
     if (string.IsNullOrEmpty(tbnumber.Text)) 
     { 
      MessageBox.Show("Please enter a number from 0-100."); 
     } 
     else 
     {/*If it is not empty, then I want the system to determine if the variable 
     that has been entered can be converted to a int.*/ 
      number = Convert.ToString(tbnumber.Text); 
      if (Int32.TryParse(number, out guess)) 
      { 
       /*If the value can be converted, then I want the system to see if 
     it is lower, higher, or equal to the random value. Then I want to record how 
       many times the user guessed.*/ 
       if (guess < randomnumber) 
       { 
        lbloutput.Text = "Too Low"; 
        this.BackColor = System.Drawing.Color.LightSeaGreen; 
        counter = counter + 1; 
       } 
       else if (guess > randomnumber) 
       { 
        lbloutput.Text = "Too High"; 
        this.BackColor = System.Drawing.Color.SlateBlue; 
        counter = counter + 1; 
       } 
       else 
       { 
        lbloutput.Text = "Good Guess"; 
        counter = counter + 1; 
        lblcounter.Text = "You guessed " + counter + " times."; 
       } 
      } 
      else 
      { 
       /*If the value cannot be converted to a int, then display a message box saying so.*/ 
       MessageBox.Show("This is not a number. Please enter a number between 0-100"); 

      } 
     } 
    } 
} 

回答

3

這樣改變你的代碼。你正試圖在不允許的類中調用Next方法,並且當你在構造方法中移動完整的代碼時,那麼你的變量作用域只存在於該構造方法中,所以在另一個方法中訪問的變量將不起作用。所以,解決辦法是在類級別定義變量,但初始化它的構造

Random random; 
    int randomnumber; 
    public Guess() 
    { 
     /*Once the program is initalized, I want the 2nd button hidden until the first one 
     is clicked with a value in the textbox*/ 

     InitializeComponent(); 
     btnexe2.Hide(); 
     random = new Random(); 
     randomnumber = random.Next(0, 101); 
    } 
+0

當我這樣做,我的「如果」語句成爲搞砸了,說明隨機‘「名’不存在於當前上下文中。我在那張貼我原來的問題說這一點。它的驅動我nutz!大聲笑 – Spr89

+0

不,我想你是移動整個代碼的構造函數,意思是你在構造函數中定義變量,所以隨機變量不會在另一種方法中被識別btnexe2_click ....你能發佈更新的答案嗎? – Viru

+0

也可以確保你在if語句中使用randomnumber而不是隨機 – Viru

0

嘗試改變這種

Random random = new Random(); 
int randomnumber = random.Next(0, 101); 

public Guess() 
{ 
    /*Once the program is initalized, I want the 2nd button hidden until the first one 
    is clicked with a value in the textbox*/ 
    InitializeComponent(); 
    btnexe2.Hide(); 
} 

這個

private Random _Random; 
private int _RandomNumbrer; 
public Guess() 
{ 

    _Random = new Random(); 
    _RandomNumbrer = random.Next(0, 101); 

    /*Once the program is initalized, I want the 2nd button hidden until the first one 
    is clicked with a value in the textbox*/ 
    InitializeComponent(); 
    btnexe2.Hide(); 
} 

您選擇幾乎沒有什麼youre設法做

+0

當我這樣做時,它與Viru的帖子一樣,我的「if」語句變得混亂了。當我使用你的下劃線時,我最終得到了一個全新的錯誤世界,不太清楚發生器如何在沒有「int」的情況下工作。 – Spr89

1

你應該初始化一個方法內的變量(初始化方法將值添加到變量 - 使用「新」關鍵字);

嘗試這樣:

class Guess { 
    Random random; 
    int randomNumber; 

    public Guess() { 
     random = new Random(); 
     randomnumber = random.Next(0, 101); 
     //Rest of the code 
    } 
} 
+0

當我這樣做時,我的所有錯誤都消失了,我得到1個新錯誤代替所有其他錯誤。現在系統說「方法必須有返回類型」。我會在「公共猜測()」方法的底部放置「隨機返回」嗎? (我認爲它是一種方法) – Spr89

+0

@ Spr89'Guess()'是一個構造方法。它沒有返回類型。你的代碼中已經有了一個構造方法,在我的答案中,我的意思是你應該將這2個初始化行添加到你已經存在的構造函數中。 –