static void Main(string[] args)
{
//简单的四则运算
do
{
try
//try catch语句处理异常
{
Console.WriteLine("请输入一个数字");
int input1 = Int32.Parse(Console.ReadLine());
Console.WriteLine("请再次输入一个数字");
int input2 = Int32.Parse(Console.ReadLine());
//这里可以两个接取的值可以用方法写在一起,但是不容易看懂
Console.WriteLine("请选择要进行的运算,输入+-*/");
string s = Console.ReadLine();
if (s == "+")
{
Console.WriteLine("{0}+{1}={2}", input1, input2, Add.add(input1, input2));
}
else if (s == "-")
{
Console.WriteLine("{0}-{1}={2}", input1, input2, Less.less(input1, input2));
}
else if (s == "*")
{
Console.WriteLine("{0}*{1}={2}", input1, input2, Cheng.cheng(input1, input2));
}
else if (s == "/")
{
Console.WriteLine("{0}/{1}={2}", input1, input2, Chu.chu(input1, input2));
}
else
{
Console.WriteLine("输入有误,请重新输入");
}
}
catch
{
Console.WriteLine("输入错误,请重新输入");
}
}
while (true);
}
public class Add
{
//static 静态修饰符,只能有静态成员访问
public static int add(int a, int b)
{
return (a + b);
}
}
public class Less
{
public static int less(int a, int b)
{
return (a - b);
}
}
public class Cheng
{
public static int cheng(int a, int b)
{
return (a * b);
}
}
public static class Chu
{
public static double chu(double a, double b)
{
return (a / b);
}
}
}
}
准备有时间在慢慢丰富,再用winform写出来
|
|