自動出題判分——c#學習實踐

ucsb發表於2017-05-19

1、程式功能自動出題—(程式隨意給出10以內加減乘除)並且判斷結果正確與否,正確打對勾,錯誤打錯號。

2、設計介面:

3、程式碼:

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;

namespace text01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int a, b;
        string op;
        int result;

        Random rnd = new Random();

        private void btnNew_Click(object sender, EventArgs e)
        {   //隨機產生A\B
            a = rnd.Next(9) + 1;
            b = rnd.Next(9) + 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;
            }
            lblA.Text = a.ToString();
            lblB.Text = b.ToString();
            lblOp.Text = op;
            txtAnswer.Text = "";

        }

        private void btnJudge_Click(object sender, EventArgs e)
        {
            string str = txtAnswer.Text;
            double d = double.Parse(str);
            string disp = "" + a + op + b + "=" + str + " ";
            if (d == result) // if( Math.Abs(d-result)< 1e-3 )
                disp += "";
            else
                disp += "X";

            lstDisp.Items.Add(disp);
        }


    }
}

4、執行成果圖:

 

相關文章