# params. params . , params, , . , . , .
static double CalcAverage(params double[] values)
{
double avarage = 0;
if (values.Length == 0) return 0;
foreach (double x in values)
avarage += x;
avarage /= values.Length;
return avarage;
}
static void Main(string[] args)
{
//1
Console.WriteLine(" ="+CalcAverage(0.2, 0.3, 0.5, 0.7));
//2
double[] mas = { 0.2, 0.3, 0.5, 0.7 };
Console.WriteLine(" =" + CalcAverage(mas));
//3
Console.WriteLine(" =" + CalcAverage());
}
CalculateAverage () params, , .. CalculateAverage(), double.
, # , params, .
. , . , , .
static void Print(string s = " ", int value = 1)
{
Console.WriteLine(s + " " + value);
}
static void Main(string[] args)
{
Print();
Print(" ");
Print(,2);//Error!!!
Print(" ",15);
}
, , .
static void Print(double [] mas,int size=mas.Length)
{
// int size = mas.Length;
for (int i = 0; i < size; i++)
Console.Write(mas[i] + " ");
Console.WriteLine();
}
static void Main(string[] args)
{
double[] mas = { 0.2, 0.3, 0.5, 0.7 };
Print(mas);
}
Error .. time .
. , , , ( ), , . .
|
|
static void DisplayMessage(ConsoleColor textColor,ConsoleColor backgroundColor, string message)
{
//
// .
ConsoleColor oldTextColor = Console.ForegroundColor;// - , ..
ConsoleColor oldbackgroundColor = Console.BackgroundColor;// -
// .
Console.ForegroundColor = textColor;
Console.BackgroundColor = backgroundColor;
Console.WriteLine(message);
// .
Console.ForegroundColor = oldTextColor;
Console.BackgroundColor = oldbackgroundColor;
}
static void Main(string[] args)
{
DisplayMessage (message: " ", textColor: ConsoleColor.DarkRed, backgroundColor: ConsoleColor.White);
DisplayMessage(backgroundColor: ConsoleColor.Green,message: " ...", textColor: ConsoleColor.DarkBlue);
Console.ReadLine();
// , .
DisplayMessage(ConsoleColor.Blue,message: "Testing...",backgroundColor: ConsoleColor.White);
// , .
DisplayMessage(message: "Testing...",backgroundColor: ConsoleColor.White, ConsoleColor.Blue);
}
, .
static void DisplayMessage(ConsoleColor textColor = ConsoleColor.Blue,
ConsoleColor backgroundColor = ConsoleColor.White, string message = "Test Message")
{....}
DisplayMessage(backgroundColor: ConsoleColor.Green); // , ,
//Test Message
DisplayMessage(message:"Hello"); // , , Hello
, - . , . , .
static int MaxValue(int a, int b)
{
if (a > b)
return a;
else
return b;
}
static double MaxValue(double a, double b)
{
if (a > b)
return a;
else
return b;
}
static string MaxValue(string a, string b)
{
if (String.Compare(a,b)>0)
return a;
else
return b;
}
static void Main(string[] args)
{
Console.WriteLine(" {0} {1} = {2}",1,5, MaxValue(1,5));
Console.WriteLine(" {0} {1} = {2}", 1.5, 5.1, MaxValue(1.5, 5.1));
Console.WriteLine(" {0} {1} = {2}", "111","555", MaxValue("111","555"));
}
}
Visual Studio 2010 . (, , Console.WriteLine ()) .
|
|
, - ( ) ( ) . .
:
1. , , ;
2. , , . . .
1. (n!), .
: n
: n!
n=5. n=4. n=5 :
5!=4!*5.
:
4!=3!*4;
3!=2!*3;
2!=1!*2;
1!=0!*1
() :
0!=1.
:
class Program
{
static int fact(int n)
{
if (n==0)return 1;//
return (n*fact(n-1));
}
static void Main(string[] args)
{
Console.WriteLine(" ");
int k = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0}!={1}",k,fact(k));
}
}
2. , .
: x,n
: xn
:
static int pow(int x, int y)
{
if (y == 0) return 1;
else return (x * pow(x, y - 1));
}
static void Main(string[] args)
{
Console.WriteLine(" ");
int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0}^{1}={2}", x,y, pow(x,y));
}