简易四则运算

需求分析:编译一个能随机产生10以内的自然数,并且能够根据客户需求,进行加、减、乘、除四则运算,并统计出最终计算正确与计算错误的题目个数。

思考:编写这个程序,首先要定义几个能产生随机数的变量,并且控制它的变化区间。然后就是四则运算,在这里,我采用了switch语句,以便于进行客户的需求计算,简化程序代码。最后,使用do循环来实现运算的无限循环性……

具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operation
{
class Program
{
static void Main(string[] args)
{ int i=1;
int a = 0;
int b = 0;
do{
Console.WriteLine("输入【+】进行加法运算");
Console.WriteLine("输入【-】进行加法运算");
Console.WriteLine("输入【*】进行加法运算");
Console.WriteLine("输入【/】进行加法运算");


Random num = new Random();
int d = num.Next(0, 10);
int e = num.Next(0, 10);
int h = num.Next(0, d + 1);//减数,要小于等于被减数
int f = num.Next(1, 10);//除数,不能为零
string y = Console.ReadLine();

switch (y)
{
case "+":
Console.WriteLine("{0}+{1}=?", d, e);
int result = Convert.ToInt32(Console.ReadLine());//类型转换
if (result == d + e)
{ Console.WriteLine("计算正确!");
b++;
}
else
{
Console.WriteLine("计算错误");

}
a++;
break;
case "-":
Console.WriteLine("{0}-{1}=?", d, h);
int result2 = Convert.ToInt32(Console.ReadLine());
if (result2 == d - h)
{ Console.WriteLine("计算正确!");
b++;
}
else
{
Console.WriteLine("计算错误");
}
a++;
break;
case "*":
Console.WriteLine("{0}*{1}=?", d, e);
int result3 = Convert.ToInt32(Console.ReadLine());
if (result3 == d * e)
{ Console.WriteLine("计算正确!");
b++;
}
else
{
Console.WriteLine("计算错误");
}
a++;
break;
case "/":
Console.WriteLine("{0}/{1}=?", d, f);
int result4 = Convert.ToInt32(Console.ReadLine());
if (result4 == d / f)
{ Console.WriteLine("计算正确!");
b++;
}
else
{
Console.WriteLine("计算错误");
}
a++;
break;

}
Console.WriteLine("已答对{0}题,答错{1}题!", b, a - b);//显示测试结果
}
while(i<2);


Console.ReadLine();
}
}
}

总结:通过这个小小的计算程序,我发现了自己的不足之处。

我发现自己只能做一些简单的代码编写,并且速度非常慢,就拿这个程序来说吧,加、减、乘这些很简单的代码,我可以编写出来,但是除法,我发现自己控制不了,不能控制让它产生能整除的数,也不能接收客户输入的小数,最终就导致的结果就是进行除法运算的时候常常出错,然而,我并没有找到解决的办法,但我一直没有放弃,我在互联网上搜索各种解决方案,尝试各种可能的语句。但是遗憾的是,目前还没有找到合适的方案。

因为时间限制,我只能先把这份半成品提交,不过我仍会继续改进,直到成功为止,到时,我会把这份作业再次提交。

希望老师在看到这份作业时不要失望,我会提高自己,争取做出真正理想的程序。

posted on 2015-10-05 17:08  鱼无泪  阅读(170)  评论(1)    收藏  举报