using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static int sun;
private static int x, y, z;
private static string f;
private static string h;
static void Main(string[] args)
{
Console.Write("请输入随机数的范围(9-10):");
string azzz = Console.ReadLine();
string regex = @"(\d+)\D+(\d+)";
Match mstr = Regex.Match(azzz, regex);
int s1 = int.Parse(mstr.Groups[1].Value);
int s2 = int.Parse(mstr.Groups[2].Value);
random(s1,s2);
Console.Write("请输入答案:");
int l = int.Parse(Console.ReadLine());
rightanswer(f, h, x, y, z);
if (l == sun)
{
Console.WriteLine("回答正确!");
}
else
{
Console.WriteLine("回答错误!");
}
Console.Read();
}
private static void random(int a,int b)
{
Random si = new Random();
x = si.Next(a, b);
y = si.Next(a, x);
z = si.Next(a, y);
string[] operators = new string[] { "+", "-", "*", "/" };
f = operators[new Random().Next(0, 4)];
h = operators[new Random().Next(0, 4)];
string z1 = "=";
string xfyhz = x + f + y + h + z + z1;
Console.WriteLine(xfyhz);
}
private static int rightanswer(string a1, string a2, int c, int d, int e)
{
string a12 = a1 + a2;
switch (a12)
{
case "++":
sun = c + d + e;
break;
case "+-":
sun = c + d - e;
break;
case "-+":
sun = c - d + e;
break;
case "--":
sun = c - d - e;
break;
case "-*":
sun = c - d * e;
break;
case "+*":
sun = c + d + e;
break;
case "*/":
sun = c * d / e;
break;
case "**":
sun = c * d * e;
break;
case "*-":
sun = c + d - e;
break;
case "*+":
sun = c * d + e;
break;
case "/*":
sun = c / d * e;
break;
case "/-":
sun = c / d - e;
break;
case "/+":
sun = c / d + e;
break;
case "//":
sun = c / d / e;
break;
case "-/":
sun = c - d / e;
break;
case "+/":
sun = c + d / e;
break;
}
return sun;
}
}
}
总结:
改了很长时间,真是一点细微的细节都能决定程序的运行是否成功!写代码应该仔细,仔细,再仔细!