/// <summary> /// 写一个将鼠标点击事件 将字符串转换成相应数值的方法 /// </summary> /// <param name="str">玩家点击的按钮内容</param> /// <returns>按钮相对应的整数值</returns> public int ChangeStrToInt(string str) { int n = 0; //对传入的字符串进行判断 switch (str) { case "剪刀": n = 1; break; case "石头": n = 2; break; case "布": n = 3; break; default: throw new Exception("未知出法"); } return n; }
/// <summary> /// 写一个电脑随机出拳的方法 /// </summary> /// <returns>电脑的出拳值</returns> public int PC() { Random ran = new Random(); //random.Next方法 取最小值,但不取最大值, 所以在1-3内随机取值 int vPC = ran.Next(1, 4); string strPC = string.Empty; //进行简单判定 获取的随机值 同时转换成相应的字符串 switch (vPC) { case 1: strPC = "剪刀"; break; case 2: strPC = "石头"; break; case 3: strPC = "布"; break; default: throw new Exception("未知错误:无法判断玩家出的拳!"); } lblPC.Text = strPC;//显示电脑出的拳 return vPC; }