| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 499 人关注过本帖
标题:Number Guessing Game。
只看楼主 加入收藏
hn626589
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-7-25
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:3 
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 出了问题
希望版主尽快解决。
搜索更多相关主题的帖子: Game Number Guessing 
2009-07-25 00:24
prankmoon
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:161
专家分:921
注 册:2009-7-21
收藏
得分:0 
以下是引用hn626589在2009-7-25 00:24的发言:

希望版主尽快解决。 ...


那你就耐心等待版主吧。
2009-07-25 00:35
hn626589
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-7-25
收藏
得分:0 
回复 2楼 prankmoon
别开玩笑啦,你知道我是希望各路高手帮忙的。
2009-07-25 00:44
wxjeacen
Rank: 7Rank: 7Rank: 7
等 级:禁止访问
帖 子:1291
专家分:628
注 册:2009-3-22
收藏
得分:100 
我这里有一个带图形界面的。

你可以拿去参考一下,QT的。

GuessNumber.tar.gz (29.21 KB)

生命不熄,战斗不止.
2009-07-25 09:30
快速回复:Number Guessing Game。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020423 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved