首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

剪子石头布求代码优化

2013-01-17 
剪刀石头布求代码优化如题,以下是代码,感觉特别不简洁,虽然能运行.本人新入门C#求指点.谢谢private void W

剪刀石头布求代码优化
如题,以下是代码,感觉特别不简洁,虽然能运行.        本人新入门C#求指点.谢谢
private void Winlost(int Cpuhand,string Userhand)//形参,电脑,人出手情况
        {//电脑 :0剪刀1石头2布   人:j剪刀s石头b布
            if(this.cpuhand==0)//如果电脑出剪刀,对比人的三种情况
            {
                if (this.userhand == "j")
                {
                    Console.WriteLine("平");
                }
                if (this.userhand == "s")
                {
                    this.userwin++;
                    Console.WriteLine("你赢了);
                }
                if (this.userhand == "b")
                {
                    this.cpuwin++;
                    Console.WriteLine("电脑赢);
                }
            }
            if (this.cpuhand == 1)//如果出布,对比
            {
                if (this.userhand == "s")
                {
                    Console.WriteLine("平");
                }
                if (this.userhand == "b")
                {
                    this.userwin++;
                    Console.WriteLine("你赢了);
                }
                if (this.userhand == "j")
                {
                    this.cpuwin++;
                    Console.WriteLine("电脑赢);


                }
            }
            if (this.cpuhand == 2)//如果出石头,对比
            {
                if (this.userhand == "b")
                {
                    Console.WriteLine("平");
                }
                if (this.userhand == "j")
                {
                    this.userwin++;
                    Console.WriteLine("你赢了);
                }
                if (this.userhand == "s")
                {
                    this.cpuwin++;
                    Console.WriteLine("电脑赢);
                }
            }
        }
[解决办法]
人手电脑都统一编成数字,python代码

a = ((0,1,2),
     (2,0,1),
     (1,2,0))
b = ("平","你赢了","电脑赢")
print(b[a[cpuhand][playerhand]])

热点排行