| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 826 人关注过本帖
标题:麻烦大家看看我这程序问题出在哪?
只看楼主 加入收藏
zhysh926088
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-11-25
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:6 
麻烦大家看看我这程序问题出在哪?
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
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:7 
哪一行有错?什么错?

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2011-11-25 11:52
yinniannian
Rank: 9Rank: 9Rank: 9
来 自:河北省石家庄
等 级:蜘蛛侠
威 望:2
帖 子:256
专家分:1007
注 册:2011-5-13
收藏
得分:7 
把错误贴出来

代做小型软件。
QQ:449795473
2011-11-25 16:56
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
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:0 
没有转化成字符串?

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2011-11-26 21:10
祜风
Rank: 1
来 自:湖北荆州
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-11-26
收藏
得分:0 
我也是新手,看了一下,你的ownkey(int[] roomlist,int room,int[] keylist)中得key是整型的却没有转换后在赋值给 string a,前面一个问题不知道。
2011-12-02 12:58
快速回复:麻烦大家看看我这程序问题出在哪?
数据加载中...
 
   



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

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