| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 826 人关注过本帖
标题:麻烦大家看看我这程序问题出在哪?
取消只看楼主 加入收藏
zhysh926088
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-11-25
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
麻烦大家看看我这程序问题出在哪?
namespace fifth
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "";
            int key = Convert.ToInt32(textBox1.Text);
            int[] roomlist = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int[] keylist = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            for (int i = 0; i < 10; i++)
            {
                openroom(keylist, key, roomlist);
            }
            
        }
        private int openroom(int[] keylist,int key,int[] roomlist)
        {
            for(int i=0;i<10;i++)
            {
                if (key == roomlist[i])
                    {
                         string a = "我是" + key + "的钥匙,能打开" + roomlist[i] + "房间的门。" ;
                         label2.Text = label2.Text + a ;
                         keylist[key - 1] = -1;
                         key=ownkey(roomlist,roomlist[i],keylist);
                     }
            }
            
            return key;
        }
        private int ownkey(int[] roomlist,int room,int[] keylist)
        {
            Random rd = new Random();
            int key=0;
            Boolean b = true;
            while(b)
            {
                key = (int)rd.Next(0, 11);
            for (int i = 1; i < 10;i++ )
                {
                    if (key == keylist[i])
                    {
                       
                        b = false;
                        break;
                    }
                }
            }
            string a = "我是房间" + room + ",我有" + key + "钥匙。";
            label2.Text =label2.Text +a + "\n";
            roomlist[room - 1] = -1;
            return key;
        }

    }
}
搜索更多相关主题的帖子: public private 
2011-11-25 10:29
zhysh926088
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-11-25
收藏
得分:0 
回复 2楼 jedypjd
namespace fifth
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "";
            int key = Convert.ToInt32(textBox1.Text);
            int[] roomlist = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int[] keylist = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            for (int i = 0; i < 10; i++)//应该是循环十次,结果却是运行少于十次的不确定次。
            {
                openroom(keylist, key, roomlist);
            }
            
        }
        private int openroom(int[] keylist,int key,int[] roomlist)
        {
            for(int i=0;i<10;i++)
            {
                if (key == roomlist[i])
                    {
                         string a = "我是" + key + "的钥匙,能打开" + roomlist[i] + "房间的门。" ;
                         label2.Text = label2.Text + a ;
                         keylist[key - 1] = -1;
                         key=ownkey(roomlist,roomlist[i],keylist);
                     }
            }
            
            return key;
        }
        private int ownkey(int[] roomlist,int room,int[] keylist)
        {
            Random rd = new Random();
            int key=0;
            Boolean b = true;
            while(b)
            {
                key = (int)rd.Next(0, 11);
            for (int i = 1; i < 10;i++ )
                {
                    if (key == keylist[i])
                    {
                       
                        b = false;
                        break;
                    }
                }
            }
            string a = "我是房间" + room + ",我有" + key + "钥匙。";//此处说是局部变量key没有赋初值
            label2.Text =label2.Text +a + "\n";
            roomlist[room - 1] = -1;
            return key;
        }

    }
}
整个程序运行时应该循环十次,但是运行结果却是少于十次的不确定次。
2011-11-25 19:33
zhysh926088
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-11-25
收藏
得分:0 
回复 3楼 yinniannian
namespace fifth
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "";
            int key = Convert.ToInt32(textBox1.Text);
            int[] roomlist = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int[] keylist = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            for (int i = 0; i < 10; i++)//应该是循环十次,结果却是运行少于十次的不确定次。
            {
                openroom(keylist, key, roomlist);
            }
            
        }
        private int openroom(int[] keylist,int key,int[] roomlist)
        {
            for(int i=0;i<10;i++)
            {
                if (key == roomlist[i])
                    {
                         string a = "我是" + key + "的钥匙,能打开" + roomlist[i] + "房间的门。" ;
                         label2.Text = label2.Text + a ;
                         keylist[key - 1] = -1;
                         key=ownkey(roomlist,roomlist[i],keylist);
                     }
            }
            
            return key;
        }
        private int ownkey(int[] roomlist,int room,int[] keylist)
        {
            Random rd = new Random();
            int key=0;
            Boolean b = true;
            while(b)
            {
                key = (int)rd.Next(0, 11);
            for (int i = 1; i < 10;i++ )
                {
                    if (key == keylist[i])
                    {
                       
                        b = false;
                        break;
                    }
                }
            }
            string a = "我是房间" + room + ",我有" + key + "钥匙。";//此处说是局部变量key没有赋初值
            label2.Text =label2.Text +a + "\n";
            roomlist[room - 1] = -1;
            return key;
        }

    }
}
整个程序运行时应该循环十次,但是运行结果却是少于十次的不确定次。
2011-11-25 19:34
快速回复:麻烦大家看看我这程序问题出在哪?
数据加载中...
 
   



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

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