2条回答 默认 最新
- 真的小白啊 2021-06-20 23:40关注
随便写了一下
using System; using System.ComponentModel; using System.Drawing; using System.Reflection; using System.Windows.Forms; namespace WinFormsApp1 { public partial class Form1 : Form { private readonly Random RandomAnswer; private readonly string AIAnswer; public Form1() { InitializeComponent(); RandomAnswer = new Random(); AIAnswer = "电脑:"; } private void btnRock_Click(object sender, EventArgs e) { RSPGame(ChoiceType.Rock); radioButtonRock.Checked = true; } private void btnScissors_Click(object sender, EventArgs e) { RSPGame(ChoiceType.Scissors); radioButtonScissors.Checked = true; } private void btnPaper_Click(object sender, EventArgs e) { RSPGame(ChoiceType.Paper); radioButtonPaper.Checked = true; } private void RSPGame(ChoiceType choice) { ChoiceType answer = GenerateAnswer(); if (choice == answer) { labelAnswer.Text = "="; labelAnswer.ForeColor = Color.Blue; } else if (choice == ChoiceType.Rock && answer == ChoiceType.Scissors || choice == ChoiceType.Scissors && answer == ChoiceType.Paper || choice == ChoiceType.Paper && answer == ChoiceType.Rock) { labelAnswer.Text = "√"; labelAnswer.ForeColor = Color.Green; } else { labelAnswer.Text = "X"; labelAnswer.ForeColor = Color.Red; } labelAIAnswer.Text = AIAnswer + GetDescription(answer); } private string GetDescription(ChoiceType answer) { var field = answer.GetType().GetField(answer.ToString()); var attribute = field.GetCustomAttribute<DescriptionAttribute>(); return attribute.Description; } private ChoiceType GenerateAnswer() { return (ChoiceType)(RandomAnswer.Next() % 3); } } internal enum ChoiceType { [Description("石头")] Rock, [Description("剪刀")] Scissors, [Description("布")] Paper } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥50 汇编语言除法溢出问题
- ¥65 C++实现删除N个数据列表共有的元素
- ¥15 Visual Studio问题
- ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
- ¥20 求一个html代码,有偿
- ¥100 关于使用MATLAB中copularnd函数的问题
- ¥20 在虚拟机的pycharm上
- ¥15 jupyterthemes 设置完毕后没有效果
- ¥15 matlab图像高斯低通滤波
- ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗