首先,我想說我仍然是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");
}
}
}
}
當我這樣做,我的「如果」語句成爲搞砸了,說明隨機‘「名’不存在於當前上下文中。我在那張貼我原來的問題說這一點。它的驅動我nutz!大聲笑 – Spr89
不,我想你是移動整個代碼的構造函數,意思是你在構造函數中定義變量,所以隨機變量不會在另一種方法中被識別btnexe2_click ....你能發佈更新的答案嗎? – Viru
也可以確保你在if語句中使用randomnumber而不是隨機 – Viru