题目:让用户自行输入三个整数,然后输出三个数中居中的数,并输出其阶乘。
//第一种方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请输入三个数,用回车键确认");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
int middle = 0;
//a是中间值,则有两种情况,b是最小值或b是最大值
//b同
//c同
if ((a >= b && a <= c) || (a >= c && a <= b))
{
Console.WriteLine("这三个数中间数是{0}", a);
middle = a;
}
if (b >= a && b <= c || b >= c && b <= a)
{
Console.WriteLine("这三个数中间数是{0}", b);
middle = b;
}
if (c >= a && c <= b || c >= b && c <= a)
{
Console.WriteLine("这三个数中间数是{0}", c);
middle = c;
}
double result = 1;
for (int i = 1; i <= middle; i++)
{
result *= i;
}
Console.WriteLine("它的阶乘是" + result);
}
Console.ReadKey();
}
}
}
//第二种方法
第二种方法与第三种方法区别在于应用了return的两种不同形式,第二种方法是double类型,有返回值,第三种方法是void类型,无返回值,两者调用方法不同。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入三个数,用回车键确认");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
if(a<b)
{
if(b<c)
{
Console.WriteLine("这三个数中间数是{0}",b);
Console.WriteLine("此数阶乘为{0}", jiechen(b));
}
else if(c>a)
{
Console.WriteLine("这三个数中间数是{0}", c);
Console.WriteLine("此数阶乘为{0}", jiechen(c));
}
else
{
Console.WriteLine("这三个数中间数是{0}", a);
Console.WriteLine("此数阶乘为{0}", jiechen(a));
}
}
else if(b>c)
{
Console.WriteLine("这三个数中间数是{0}", b);
Console.WriteLine("此数阶乘为{0}", jiechen(b));
}
else if(c>a)
{
Console.WriteLine("这三个数中间数是{0}", a);
Console.WriteLine("此数阶乘为{0}", jiechen(a));
}
else
{
Console.WriteLine("这三个数中间数是{0}", c);
Console.WriteLine("此数阶乘为{0}", jiechen(c));
}
Console.ReadKey();
}
static double jiechen(int j)
{
double p = 1;
for (int i = j; i>0; i--)
{
p *= i;
}
return p;
}
}
}
//第三种方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入三个数,用回车键确认");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
if (a < b)
{
if (b < c)
{
Console.WriteLine("这三个数中间数是{0}", b);
jiechen(b);
}
else if (c > a)
{
Console.WriteLine("这三个数中间数是{0}", c);
jiechen(c);
}
else
{
Console.WriteLine("这三个数中间数是{0}", a);
jiechen(a);
}
}
else if (b > c)
{
Console.WriteLine("这三个数中间数是{0}", b);
jiechen(b);
}
else if (c > a)
{
Console.WriteLine("这三个数中间数是{0}", a);
jiechen(a);
}
else
{
Console.WriteLine("这三个数中间数是{0}", c);
jiechen(c);
}
Console.ReadKey();
}
static void jiechen(int j)
{
double p = 1;
for (int i = j; i > 0; i--)
{
p *= i;
}
Console.WriteLine("此数阶乘为{0}", p);
return;
}
}
}