none
不知道到底错在哪里,为什么n++无效。 RRS feed

  • 常规讨论

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace 猜数字
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Box1_TextChanged(object sender, EventArgs e)
            {
            }

            private void btn_Click(object sender, EventArgs e)
            {
                int rndint = 50;
                int n=0;
                while (true)
                {
                        if (Convert.ToInt32(Box1.Text) > rndint)
                        {
                            Label3.Text = ("第" + n + "次,大了");
                            n++;
                           return;
                        }
                        else if (Convert.ToInt32(Box1.Text) < rndint)
                        {
                            Label3.Text = ("第" + n + "次,小了");
                            n++;
                            return;
                        }
                        else if (Convert.ToInt32(Box1.Text) == rndint)
                        {
                            Label3.Text = ("恭喜你,猜中了!共尝试了" + n + "次");
                            break;
                        }
                }
                    DialogResult result;
                    result = MessageBox.Show("再玩一次?","猜数字小游戏", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                          return;
                    }
                    else
                    {
                       Application.Exit();
                    }
            }
        }
    }

    2012年10月25日 11:41

全部回复

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace 猜数字
    {
        public partial class Form1 : Form
        {
            int n=0;
            public Form1()
            {
                InitializeComponent();
            }
            private void Box1_TextChanged(object sender, EventArgs e)
            {
            }
            private void btn_Click(object sender, EventArgs e)
            {
                int rndint = 50;
                
                while (true)
                {
                        if (Convert.ToInt32(Box1.Text) > rndint)
                        {
                            Label3.Text = ("第" + n + "次,大了");
                            n++;
                           return;
                        }
                        else if (Convert.ToInt32(Box1.Text) < rndint)
                        {
                            Label3.Text = ("第" + n + "次,小了");
                            n++;
                            return;
                        }
                        else if (Convert.ToInt32(Box1.Text) == rndint)
                        {
                            Label3.Text = ("恭喜你,猜中了!共尝试了" + n + "次");
                            break;
                        }
                }
                    DialogResult result;
                    result = MessageBox.Show("再玩一次?","猜数字小游戏", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                          n=0;
                          return;
                    }
                    else
                    {
                       Application.Exit();
                    }
            }
        }
    }

    n应该设置成全局变量。

    2012年10月26日 1:15