要求:编写一个能对0-10之间的整数进行四则运算的软件,程序能接收用户输入的答案,并判断对错,程序结束时,统计出答对,答错的题目数量。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入你想做的运算+,-,*,/ ");
string m = Console.ReadLine() ;
switch (m)
{
case "+":
Shu.Wo();
break;
case "-":
Shu.Sub();
break;
case "*":
Shu.Sheng();
break;
case "/":
Shu.Chu();
break;
}
Console.ReadLine();
}
}
//定义委托
public delegate double Num(double x,double y);
public class Math
{
public static double Add(double x, double y)
{ return x + y; }
public static double Sub(double x, double y)
{ return x - y; }
public static double Sheng(double x, double y)
{ return x * y; }
public static double Chu(double x, double y)
{ return x / y; }
}
public class Shu
{
public static int i = 0;
public static int right = 0;
public static void Wo()
{
double sum, n;
Random r = new Random();
double x = r.Next(0,10);
double y= r.Next(0, 10);
sum = x + y;
Console.WriteLine("{0}+{1}=",x,y);
Console.WriteLine("请输入你的结果:");
n=double.Parse( Console.ReadLine());
Num nu = new Num(Math.Add);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}+{1}={2}",x,y,nu(x,y));
if (sum == n)
{
Console.WriteLine("你的结果正确!");
i++;
right++;
Wo();
}
else if (n == 99)
{
Jieshu();
}
else
{
Console.WriteLine("你的结果不正确");
i++;
Wo();
}
}
public static void Jieshu()
{
Console.WriteLine("你好,你已退出计算!你公做{0}道题,答对{1}",i,right);
}
public static void Sub()
{
double sum, n;
Random r = new Random();
double x = r.Next(0, 10);
double y = r.Next(0, 10);
sum = x - y;
Console.WriteLine("{0}-{1}=", x, y);
Console.WriteLine("请输入你的结果:");
n = double.Parse(Console.ReadLine());
Num nu = new Num(Math.Sub);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}-{1}={2}", x, y, nu(x, y));
if (sum == n)
{
Console.WriteLine("你的结果正确!");
i++;
right++;
Wo();
}
else if (n == 99)
{
Jieshu();
}
else
{
Console.WriteLine("你的结果不正确");
i++;
Sub();
}
}
public static void Sheng()
{
double sum, n;
Random r = new Random();
double x = r.Next(0, 10);
double y = r.Next(0, 10);
sum = x *y;
Console.WriteLine("{0}*{1}=", x, y);
Console.WriteLine("请输入你的结果:");
n = double.Parse(Console.ReadLine());
Num nu = new Num(Math.Sheng);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}*{1}={2}", x, y, nu(x, y));
if (sum == n)
{
Console.WriteLine("你的结果正确!");
i++;
right++;
Wo();
}
else if (n == 99)
{
Jieshu();
}
else
{
Console.WriteLine("你的结果不正确");
i++;
Sheng();
}
}
public static void Chu()
{
double sum, n;
Random r = new Random();
double x = r.Next(0, 10);
double y = r.Next(0, 10);
sum = x / y;
Console.WriteLine("{0}/{1}=", x, y);
Console.WriteLine("请输入你的结果:");
n = double.Parse(Console.ReadLine());
Num nu = new Num(Math.Chu);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}/{1}={2}", x, y, nu(x, y));
if (sum == n)
{
Console.WriteLine("你的结果正确!");
i++;
right++;
Wo();
}
else if (n == 99)
{
Jieshu();
}
else
{
Console.WriteLine("你的结果不正确");
i++;
Chu();
}
}
}
}