您的位置:首页 > 其它

窗体应用程序:四则运算

2015-10-02 19:06 225 查看
1、需求分析

编写一个能对0--10之间随机生成的整数进行四则运算的“软件” 程序能接收用户输入的整数答案,并判断对错 程序结束时,统计出答对、答错的题目数量。

2、具体设计思路

首先想到,既然要用四种方法,就用switch case 语句来根据用户的选择进行其中的一种运算。算法直接嵌套在case语句里,这样既简单又方便。
要求进行运算的两个数是随机的,用Random产生两个0~10的随机数。
共需要两个窗体,一个进行四则运算,一个进行统计。
定义一个count来进行存储答题数目,定义一个right来进行存储答对数目。

3、编写过程截图





4.具体代码实现

Form1.cs:

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 四则运算
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int Count = 0;
public static int right = 0;
private void RDN()
{
Random rd = new Random();
int n1, n2;
n1 = rd.Next(0, 11);
n2 = rd.Next(0, 11);
textBox1.Text = n1.ToString();
textBox2.Text = n2.ToString();
textBox3.Text = "";
}

private void button1_Click(object sender, EventArgs e)
{
label1.Text = button1.Text;
RDN();
}

private void button2_Click(object sender, EventArgs e)
{
label1.Text = button2.Text;
RDN();
}

private void button3_Click(object sender, EventArgs e)
{
label1.Text = button3.Text;
RDN();
}

private void button4_Click(object sender, EventArgs e)
{
label1.Text = button4.Text;
RDN();
}

private void button5_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}

private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
int result = 0;
string s = label1.Text;

switch (s)
{
case "+":
result = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
break;
case "-":
result = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
break;
case "×":
result = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
break;
case "÷":
if (textBox2.Text=="0")
{
MessageBox.Show("分母为0,不计入答题总数,请回车继续答题!");
}
else
{
result = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
}
break;
}
if (e.KeyCode == Keys.Enter)
{
if (textBox3.Text == result.ToString())
{
right++;
Count++;
MessageBox.Show("回答正确!");
}
else
{
if (textBox2.Text=="0")
{
RDN();
}
else
{
MessageBox.Show("答题错误!");
RDN();
Count++;
}
}
RDN();
}

}
}
}


Form2.cs:

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 四则运算
{
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.Count - Form1.right).ToString();
}

}
}


5、运行截图









6、总结

通过这次的作业,我发现,“分析”很重要啊。我们要对用户的需求来进行分析,列出具体的框架。
当我们进行测试的时候,如果达不到效果,我们还要反过来进行分析,到底是哪里出了错。
经过一系列的分析,测试,来达到我们最终的效果!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: