zoukankan      html  css  js  c++  java
  • 四则运算程序

    程序目的:编写一个能对0-10之间的整数进行四则运算的程序。

    程序内容:系统在一分钟内随机生成0-10之间的数字,用户通过选择四则运算中的一个进行运算,并输入相应的答案,程序

                     能接收用户输入的整数答案,并进行判断,输出答题的正确、错误个数。

       设计思路:运用windows窗体应用程序,用户能很容易的看懂操作步骤,

       需要控件:

                    窗体1:4个label控件,4个textBox控件,6个Button控件,1个GroupBox控件。

                    窗体2:3个label控件,3个textBox控件。

                     主要排布如下图所示:

                      

        窗体1的代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace jisuan
    {
        public partial class Form1 : Form
        {
            
            public static int Count = 0;    //题目的总数
            private int t = 60;             //测试时间为60秒
            public static int right = 0;    //正确题目的总数
            public static int fault = 0;    //错误题目的总数
            public Form1()
     
            {
                InitializeComponent();
            }
            //"开始"按钮单击事件
            private void button1_Click(object sender, EventArgs e)
            {
                label4.Text = t.ToString();
                timer1.Enabled = true;
                timer1.Interval = 1000;
                timer1.Start();
                RandomNum();
    
            }
            //自定义方法:产生1-10的随机数并在文本框中显示
            private void RandomNum()
            {
                Random ran = new Random();
                int x1, x2;
                x1 = ran.Next(1, 10);      //textBox1产生一个数字
                x2 = ran.Next(1, 10);      //textBox2产生一个数字
                textBox1.Text = x1.ToString();
                textBox2.Text = x2.ToString();
                textBox3.Text = "";
                Count++;
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                label2.Text = "+";
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                label2.Text = "-";
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                label2.Text = "*";
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                label2.Text = "/";
            }
    
            private void textBox3_KeyDown(object sender, KeyEventArgs e)
            {  if (e.KeyCode == Keys.Enter)
                {
                    if (textBox3.Text == textBox3.ToString())
                        right++;
                   
                    RandomNum();
                }
                else
                {
                     fault++;
                    RandomNum();
                }
                int t3;
                string i = label2.Text;
                switch (i)
                { 
                    case"+":
                        t3 =int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
                        break;
                    case "-":
                        t3 = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
                        break;
                    case "*":
                        t3 = int.Parse(textBox1.Text)* int.Parse(textBox2.Text);
                        break;
                    case "/":
                        t3 = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
                        break;
                }
              
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                
                  if (t<=0)
                {
                    timer1.Enabled = false;
                    textBox3.Enabled = false;
                    MessageBox.Show("时间到!此次测试结束。");
                    textBox3.Enabled = false;
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();                      //弹出模式对话框,显示测试结果
                }
                t = t -1;
                label4.Text = t.ToString();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                textBox3.Enabled = false;
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
            }
        }

     窗体2的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace jisuan
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                textBox1.Text = Form1.Count.ToString();
                textBox2.Text = Form1.right.ToString();
                textBox3.Text = Form1.fault.ToString();
            }
    
          
        }
    }

    运行以后:

     

    大概思路就是以上的代码;

    PSP耗时:计划12小时写好,实际比计划多,原来C#老师教了一个和这个类似的例题,就跟着这个思路写的,

                 写到中间种种问题都出来了,有错误,运行不了,还找不出来.经过询问同学等等,才把问题解决,

                 直到现在还是感觉中间还有很多问题,不过,对于刚开始的没思路,还是好很多的!!

    思考题:如果用户想算1-100的话,只需要更改

             int x1, x2;

                 x1 = ran.Next(1, 100); 
                 x2 = ran.Next(1, 100);

  • 相关阅读:
    centos7的变化(转)
    配置邮件报警功能(脚本方式)
    临时和永久关闭Selinux
    centos7.2安装apache比较简单,直接上代码
    zabbix--------配置邮件报警功能---服务器上配置---------
    初来驾到学java修饰符的使用
    面向对象小小理解
    出来驾到学java3
    出来驾到学java2
    初来驾到学JAVA
  • 原文地址:https://www.cnblogs.com/lx123456/p/4854830.html
Copyright © 2011-2022 走看看