Number Guessing Game。
这个猜数字游戏规则是这样:电脑随机产生一个4位数。用户从TEXT BOX 里 输入。如果对:显示"0" 如果错:显示"X" 如果位置不对:显示:"P"
我已经编了大部分,现在遇到一个问题,希望能尽快解决。
当电脑随机产生1843(只是举例),而我键入8888. ListBox 里面显示 的 是:P 0 P P.而我想要的是:X 0 X X
现把我的code 粘上:
int intE1;
int intE2;
int intE3;
int intE4;
int intEnterNumbers;
//declare variables of EnterNumbers
int intR1;
int intR2;
int intR3;
int intR4;
//declare variables of RandomNumbers
string string1 = "W";
string string2 = "W";
string string3 = "W";
string string4 = "W";
//declare variables used in listbox
intEnterNumbers = Int32.Parse(txtEnterNumbers.Text);
txtEnterNumbers.Clear();
txtEnterNumbers.Focus();
//Clear the txtbox after every time u entered
lblDisplay.Text = Convert.ToString(intRandom);
/*I set lblDisplay's visible property to invisible.
But this label display me the answer to check whether my application works*/
if (intEnterNumbers > 9999)/*Another if condition also can work:
if(intEnterNumbers/1000>=10)
Like:10000/1000=10
11111/1000=11*/
{
MessageBox.Show("Fuck u,enter 4-digit Number lar", "Exclamation",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
intE1 = intEnterNumbers / 1000;
intE2 = intEnterNumbers / 100 % 10;
intE3 = intEnterNumbers / 10 % 10;
intE4 = intEnterNumbers % 10;
//Breaking the EnterNumbers into 4 seperate Numbers
intR1 = intRandom / 1000;
intR2 = intRandom / 100 % 10;
intR3 = intRandom / 10 % 10;
intR4 = intRandom % 10;
//Breaking the RandomNumbers into 4 seperate Numbers
if (intNumberOfGuessings > 5)/*Limit the Numbers of Guessings up to 8 times*/
{
MessageBox.Show("Lol,Dump donkey,u fail.Press OK Button to Restart", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Restart();
}
else /*(intNumbersOfGuessings<=5&&intNumbersOfGuessings>=0)*/
{
if (intE1 == intR1)
{
string1 = "0";
}
else if (intE1 == intR2 || intE1 == intR3 || intE1 == intR4)
{
string1 = "P";
} else //(intE1!=intR1)
{
string1 = "X";
}
if (intE2 == intR2)
{
string2 = "0";
}
else if (intE2 == intR1 || intE2 == intR3 || intE2 == intR4)
{
string2 = "P";
}
else //(intE2!=intR2)
{
string2 = "X";
}
if (intE3 == intR3)
{
string3 = "0";
}
else if (intE3 == intR1 || intE3 == intR2 || intE3 == intR4)
{
string3 = "P";
}
else //(intE3!=intR3)
{
string3 = "X";
}
if (intE4 == intR4)
{
string4 = "0";
}
else if (intE4 == intR1 || intE4 == intR2 || intE4 == intR3)
{
string4 = "P";
}
else //(intE4!=intR4)
{
string4 = "X";
}
if (listBox1.Items.Count==0) /*I learn this way in class average:
if(listBox1.Items.Count==0)
{
listBox1.Items.Add("Number Entered:" + "\t" + "1st" + "\t" + "2nd" + "\t" + "3rd" + "\t" + "4th");
}
I find this way better:
because when you use if(intNumberOfGuessings==1)....
when you first type Non-number or Blank,
the intNumberOfGuessings also become 1,then next time
you click,listBox1 wont have the first title row.
*/
{
listBox1.Items.Add("Number Entered:" + "\t" + "1st" + "\t" + "2nd" + "\t" + "3rd" + "\t" + "4th");
listBox1.Items.Add(intEnterNumbers + "==>" + "\t" + string1 + "\t" + string2 + "\t" + string3 + "\t" + string4);
}
else //(intNumberOfGuessings>1)
{
listBox1.Items.Add(intEnterNumbers + "==>" + "\t" + string1 + "\t" + string2 + "\t" + string3 + "\t" + string4);
}
if (intE1 == intR1 &&
intE2 == intR2 &&
intE3 == intR3 &&
intE4 == intR4)
{
lblRandom1.Text = Convert.ToString(intR1);
lblRandom2.Text = Convert.ToString(intR2);
lblRandom3.Text = Convert.ToString(intR3);
lblRandom4.Text = Convert.ToString(intR4);
/*Only Display the Random Number initialise at the end*/
MessageBox.Show("Congratulations!!Press OK Button to Restart.Numbers of Guessing:" + Convert.ToString(intNumberOfGuessings),
"Congratulation!!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
windowsFormsTimer.Enabled = false;
Application.Restart(); ;
}
}
我个人觉得是这一部分的 if statement 出了问题
希望版主尽快解决。