您的位置:首页 > 其它

控制台四则运算

2015-10-05 22:12 246 查看
需求分析:

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

设计思路:

以前学过类似的案例,根据老师提供的一些代码,查询了一些资料后,利用控制台应用程序来实现需求

代码设计如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace szys

{

class Program

{

static void Main(string[] args)

{

int right = 0;

int fault = 0;

do

{

Random suiji = new Random();

int x = suiji.Next(0, 11);

int y = suiji.Next(1, 3);

Console.WriteLine("用'+'-'*'/'来选择运算或者选择按回车后输入e来结束运算");

string i = Convert.ToString(Console.ReadLine());

if (i == "+")

{

Console.WriteLine("{0}+{1}=",x, y);

int q = Convert.ToInt32(Console.ReadLine());

if (q == x + y)

{

right++;

}

else

{

Console.WriteLine("回答错误!!");

fault++;

}

}

else

if (i == "-")

{

Console.WriteLine("{0}-{1}=", x, y);

int q = Convert.ToInt32(Console.ReadLine());

if (q == x - y)

{

right++;

}

else

{

Console.WriteLine("回答错误!");

fault++;

}

}

else

if (i == "*")

{

Console.WriteLine("{0}*{1}=", x, y);

int q = Convert.ToInt32(Console.ReadLine());

if (q == x * y)

{

right++;

}

else

{

Console.WriteLine("回答错误!");

fault++;

}

}

else

if (i == "/")

{

Console.WriteLine("{0}/{1}=", x, y);

int q = Convert.ToInt32(Console.ReadLine());

if (q == x / y)

{

right++;

}

else

{

Console.WriteLine("回答错误!");

fault++;

}

}

}

while (Console.ReadLine() != "e");

Console.WriteLine("恭喜你一共答对了{0}道题,正确:{1}.错误{2}", right + fault, right, fault);

Console.ReadLine();

}

}

}

PSP耗时分析



运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: