标签:c#基础
基础加强第一天
1、先搭建一个比较丑的界面
2、电脑类
public class Computer
{
/// <summary>
/// 存储出拳内容的属性
/// </summary>
public string FistName { get; set; }
//方法
public int Play()
{
//伪随机数 电脑随机出拳
Random r = new Random();
int num = r.Next(1, 4);
//判断电脑出的是什么
switch (num)
{
case 1: this.FistName = "石头"; break;
case 2: this.FistName = "剪刀"; break;
case 3: this.FistName = "布"; break;
}
return num;
}
}
3、玩家类
public class Player
{
//存储出拳的内容
//属性
public string FistName { get; set; }
//方法
public int Play(string name)
{
//把内容存储起来
this.FistName = name;
int num = 0;
switch (this.FistName)
{
case "石头": num = 1; break;
case "剪刀": num = 2; break;
case "布": num = 3; break;
}
return num;
}
}
4、裁判类
public class CaiPan
{
/// <summary>
/// 判断电脑和玩家输赢
/// </summary>
/// <param name="playNum">玩家出拳的数字结果</param>
/// <param name="computerNum">电脑出拳的数字结果</param>
/// <returns></returns>
public string Win(int playNum,int computerNum)
{
if (playNum-computerNum==-1||playNum-computerNum==2)
{
return "玩家赢了";
}
else if (playNum - computerNum == 1 || playNum - computerNum == -2)
{
return "电脑赢了";
}
else
{
return "平手了";
}
}
}
5、点击“石头”按钮开始写单击事件,此处将“剪刀”和“布”的单击事件触碰“石头”的单击事件
//石头的出拳事件
private void btnShiTou_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
StartPlay(btn.Text);
}
private void StartPlay(string name)
{
//玩家
Player p = new Player();
//出拳并获取该出拳内容的数字结果
int playerNum = p.Play(name);
lblPlay.Text = p.FistName;//出拳内容显示出来
Computer c = new Computer();
//出拳并获取该出拳内容的数字结果
int computerNum = c.Play();
lblComputer.Text = c.FistName;//显示电脑出拳的内容
//裁判
CaiPan caipan = new CaiPan();
lblResult.Text = caipan.Win(playerNum, computerNum);
}
6、结果显示
标签:c#基础
原文地址:http://blog.csdn.net/qizhichao110/article/details/44491751