控制台四则运算
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耗时分析
运行结果:
编写一个能对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耗时分析
运行结果:
相关文章推荐
- leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数
- iOS编程------集合视图之瀑布流WaterFlowLayout
- Thinkphp 加载类库import()方法
- java 作用域 public private default protected 的差别
- 黑马程序员---成长之路-----OC之基础篇OC的概述
- d3.js学习笔记(二) 柱形图
- c++静态成员
- 详细分析内存泄露
- UVa 455 - Periodic Strings【字符串】
- 黑马程序员—this关键字及单例设计模式
- 必 备 习 题 集 ( 六 )
- 二叉树的非递归遍历
- linux epoll模型
- Java多线程17:中断机制
- 单向链表的删除及插入操作(以头插入法建立单向链表)
- oracle7
- spring4+hibernate3
- Vim入门学习
- UVALive 7146 muliset<> 容器用法 防御塔
- sizeof面试题选