C#-自动判分系统

 

使用控件4个label,两个button,一个textbox,一个listbox ;

C#-自动判分系统_第1张图片

程序和图结合,应该能够理解代码所对应的控件,不多做解释。 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace AIJudgment
{
    public partial class Judement : Form
    {
        public Judement()
        {
            InitializeComponent();
        }
        int a, b;
        bool flag = true;
        string op;
        int result;
        //产生一个Random类的实例
        Random rnd = new Random();

        private void btnnew_Click(object sender, EventArgs e)
        {
            //产生三个随机数
            flag = false;
            a = rnd.Next(500) + 1;
            b = rnd.Next(500) + 1;
            int c = rnd.Next(4);
            switch (c)
            {
                case 0: op = "+"; result = a + b; break;
                case 1: op = "-"; result = a - b; break;
                case 2: op = "*"; result = a * b; break;
                case 3: op = "/"; result = a / b; break;
            }
            //显示题目
            label1.Text = a.ToString();
            label2.Text = op;
            label3.Text = b.ToString();
            txtans.Text = "";
        }

        private void btnjud_Click(object sender, EventArgs e)
        {
            string str = txtans.Text;
            if (flag)
            {
                MessageBox.Show("您没有生成题目","评分失败");
                return;
            }
            if (str == "")
            {
                MessageBox.Show("您没有输入结果","评分失败");
                return;
            }
            double d = double.Parse(str);
            string disp = "" + a + op + b + "=" + str + "";
            if (d == result) //if (Math.Abs(d - resule) < 1e-3) 
                disp += "\tAccepted";
            else
                disp += "\tWrongAnswer";
            listBox1.Items.Add(disp);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
    }
}

 

 

你可能感兴趣的:(c#学习记录,C#小程序)