qq_59508299 2021-06-20 22:27 采纳率: 33.3%
浏览 27
已采纳

有没有会c语言程序设计的大佬呀

 

  • 写回答

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
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题