qq_59508299
2021-06-20 22:27
采纳率: 100%
浏览 26

有没有会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
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 有问必答小助手 2021-06-22 14:18

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题