Function: A module capable of accomplishing a function independently.
Function four elements: input, output, function body, functional name
function definition:
(static/public) return type function name (parameter type parameter name, parameter type argument name)
{
function body
}
Call to function:
Return variable type variable name = function (argument value)
Case: Entering a number for factorial (written as a function call)
public void Jie ()
{
Console.Write ("Please enter a=");
int a = Int. Parse (Console.ReadLine ());
int jie = 1;
for (int i = 1; i <= A; i++)
{
Jie *= i;
}
Console.Write ("Factorial result is:" + Jie);
Console.ReadLine ();
}
static void Main (string[] args)
{
Program Hanshu = new program ();
First you need to initialize this class
Hanshu. Jie ();
Can be written as: With the value of the
<summary>
Find factorial
</summary>
public void Jie (int a)
{
int jie = 1;
for (int i = 1; i <= A; i++)
{
Jie *= i;
}
Console.Write ("Factorial result is:" + Jie);
Console.ReadLine ();
}
static void Main (string[] args)
{
Program Hanshu = new program ();
First you need to initialize this class
Console.Write ("Please enter a=");
int a = Int. Parse (Console.ReadLine ());
Hanshu. Jie (a);
Can be written as a value plus a return value:
<summary>
Find factorial
</summary>
public int Jie (int a)
{
int jie = 1;
for (int i = 1; i <= A; i++)
{
Jie *= i;
}
return jie;
}
static void Main (string[] args)
{
Program Hanshu = new program ();
First you need to initialize this class
Console.Write ("Please enter a=");
int a = Int. Parse (Console.ReadLine ());
Console.Write ("Factorial result is:" + Hanshu. Jie (a));
Console.ReadLine ();
Can be written as a non-pass value but with a return value:
<summary>
Find factorial
</summary>
public int Jie ()
{
Console.Write ("Please enter a=");
int a = Int. Parse (Console.ReadLine ());
int jie = 1;
for (int i = 1; i <= A; i++)
{
Jie *= i;
}
return jie;
}
static void Main (string[] args)
{
Program Hanshu = new program ();
First you need to initialize this class
Console.Write ("Factorial result is:" + Hanshu. Jie ());
Console.ReadLine ();
Example: Write a function that returns the maximum value. Call.
<summary>
Two number comparison size returns a large
</summary>
<param name= "a" ></param>
<param name= "B" ></param>
<returns></returns>
public int Max (int a, int b)
{
if (a > B)
{
return A;
}
Else
{
return b;
}
}
static void Main (string[] args)
{
Program Hanshu = new program ();
First you need to initialize this class
Console.WriteLine (Hanshu. Max (Hanshu. Max (A, B), c));
Functions can be used more than once, but can also be nested using
Refinement
namespace function
{
Class Program
{
Format 1: No reference, no return
<summary>
Add sum, no arguments, no return value
</summary>
public void Leijia ()
{
Accumulate sum
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
Console.WriteLine (sum);
Console.ReadLine ();
}
Format 2: No reference and return
public int LeiJia1 ()
{
Accumulate sum
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
return sum;
}
Format 3: With reference to return
public int LeiJia2 (int a)
{
Accumulate sum
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
return sum;
}
Format 4: There is no return
public void LeiJia3 (int a)
{
Accumulate sum
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
Console.WriteLine (sum);
Console.ReadLine ();
}
There are parameters that do not need to be received in the function body.
There is a return value indicating that I need to use this result in the following
You need to define a variable receiving of the same data type when calling a function
function, the comparison size returns the large
Public double Max (double A, double b)
{
if (a > B)
{
return A;
}
Else//a<=b
{
return b;
}
}
Functions can be nested, but functions cannot be nested
public void You ()//mailbox, no reference, no return
{
Console.Write ("Please enter e-mail:");
String yx = Console.ReadLine ();
if (YX. Contains ("@"))
{
int A=yx. IndexOf ("@");
int B=yx. LastIndexOf ("@");
if (a = = b)
{
if (!yx. StartsWith ("@"))
{
String c = Yx. Substring (a);
if (C.contains ("."))
{
String D=yx. Substring (a-1,1);
String e = Yx. Substring (A, 1);
if (d!= ".") &&e!= ".")
{
if (!yx. EndsWith ("."))
{
Console.WriteLine ("The format of the mailbox you entered is correct");
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console. WriteLine ("Email input error");
}
Console.ReadLine ();
}
public string YOU2 ()//non-parametric return
{
Console.Write ("Please enter e-mail:");
String yx = Console.ReadLine ();
if (YX. Contains ("@"))
{
int a = YX. IndexOf ("@");
int B = Yx. LastIndexOf ("@");
if (a = = b)
{
if (!yx. StartsWith ("@"))
{
String c = Yx. Substring (a);
if (C.contains ("."))
{
String d = yx. Substring (A-1, 1);
String e = Yx. Substring (A, 1);
if (d! = "." && E! = ".")
{
if (!yx. EndsWith ("."))
{
Console.WriteLine ("The format of the mailbox you entered is correct");
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
return YX;
}
There is a return of the participants
public string You3 (string yx)
{
if (YX. Contains ("@"))
{
int a = YX. IndexOf ("@");
int B = Yx. LastIndexOf ("@");
if (a = = b)
{
if (!yx. StartsWith ("@"))
{
String c = Yx. Substring (a);
if (C.contains ("."))
{
String d = yx. Substring (A-1, 1);
String e = Yx. Substring (A, 1);
if (d! = "." && E! = ".")
{
if (!yx. EndsWith ("."))
{
Console.WriteLine ("The format of the mailbox you entered is correct");
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
return YX;
}
There is no return
public void You4 (string yx)
{
if (YX. Contains ("@"))
{
int a = YX. IndexOf ("@");
int B = Yx. LastIndexOf ("@");
if (a = = b)
{
if (!yx. StartsWith ("@"))
{
String c = Yx. Substring (a);
if (C.contains ("."))
{
String d = yx. Substring (A-1, 1);
String e = Yx. Substring (A, 1);
if (d! = "." && E! = ".")
{
if (!yx. EndsWith ("."))
{
Console.WriteLine ("The format of the mailbox you entered is correct");
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
}
Else
{
Console.WriteLine ("Email input error");
}
Console.ReadLine ();
}
Add 10 points to the array.
Public double[] Jiafen (double[] score)
{
for (int i = 0; i < score. Length; i++)
{
Score[i] + = 10;
}
return score;
}
public int renzong = 0;
public int dianzong = 0;
public string Cai ()
{
Console.Write ("Please enter what you punch (scissors, stone, cloth):");
String shu = Console.ReadLine ();
if (Shu = = "Scissors" | | shu = = "Stone" | | shu = = "cloth")
{
int ren = 0;
Switch (SHU)
{
Case "Scissors":
ren = 1;
Break
Case "stone":
ren = 2;
Break
Case "cloth":
ren = 3;
Break
}
Random ran = new random ();
int dian = ran. Next (1, 4);
Switch (DIAN)
{
Case 1:
Console.WriteLine ("Computer Out Scissors");
Break
Case 2:
Console.WriteLine ("Computer Out of stone");
Break
Case 3:
Console.WriteLine ("Computer Out of cloth");
Break
}
int jie = Ren-dian;
if (Jie = = 0)
{
Return "draw in this round! ";
}
else if (Jie = = 1 | | jie = =-2)
{
Return "This round wins!" ";
}
Else
{
Return "The failure of this round!" ";
}
}
Else
{
Return "wrong input! ";
}
}
public void Tizhong ()
{
Console.Write ("Please enter gender");
string s = Console.ReadLine ();
Console.Write ("Please enter your weight");
Double T = Double. Parse (Console.ReadLine ());
Console.Write ("Please enter your height");
Double g = Double. Parse (Console.ReadLine ());
if (s = = "Male")
{
Double n = t-g + 100;
if (n > 3 && N <=-3)
{
Console.WriteLine ("You are the standard weight");
}
else if (n > 3)
{
Console.WriteLine ("Your weight is too fat");
}
Else
{
Console.WriteLine ("Your Weight is thin");
}
}
else if (s = = "female")
{
Double n = t-g + 110;
if (n > 3 && N <=-3)
{
Console.WriteLine ("You are the standard weight");
}
else if (n > 3)
{
Console.WriteLine ("Your weight is too fat");
}
Else
{
Console.WriteLine ("Your Weight is thin");
}
}
Else
{
Console.WriteLine ("Your input is incorrect");
}
Console.ReadLine ();
}
static void Main (string[] args)
{
Required: Write a function to calculate whether the weight standard
function requires three input values, gender, weight kg, height cm
Male: Height -100= weight ±3kg
Female: Height -110= weight ±3kg
Console.Write ("Please enter Gender");
string s = Console.ReadLine ();
Console.Write ("Please enter your weight");
Double t = double. Parse (Console.ReadLine ());
Console.Write ("Please enter your height");
Double g = Double. Parse (Console.ReadLine ());
if (s = = "Male")
{
Double n = t-g + +;
if (n > 3 && N <=-3)
{
Console.WriteLine ("You are the standard weight");
}
Else if (n > 3)
{
Console.WriteLine ("Your weight is overweight");
}
Else
{
Console.WriteLine ("Your Weight is thin");
}
}
Else if (s = = "female")
{
Double n = t-g + +;
if (n > 3 && N <=-3)
{
Console.WriteLine ("You are the standard weight");
}
Else if (n > 3)
{
Console.WriteLine ("Your weight is overweight");
}
Else
{
Console.WriteLine ("Your Weight is thin");
}
}
Else
{
Console.WriteLine ("Your input is incorrect");
}
Console.ReadLine ();
Program Hanshu = new program ();
Hanshu.tizhong ();
First, initialize the class you are in.
Program Hanshu = new program ();
Hanshu. Leijia ();
int sum = Hanshu. LeiJia1 ();
Add another 10 points to this value.
sum + = 10;
int sum= Hanshu. LEIJIA2 (5);
Console.WriteLine (sum);
Console.ReadLine ();
Hanshu. LEIJIA3 (5);
Console.Write ("Please enter a=");
Double A = double. Parse (Console.ReadLine ());
Console.Write ("Please enter b=");
Double b = Double. Parse (Console.ReadLine ());
Console.Write ("Please enter c=");
Double c = Double. Parse (Console.ReadLine ());
Double max = Hanshu. Max (Hanshu. Max (A, B), c);
Console.WriteLine (max);
Console.ReadLine ();
Enter your email account to determine if the format is correct
Program Han = new program ();
Han.you ();
String yx = Han.you2 ();
Console.WriteLine (YX);
Console.ReadLine ();
Console.WriteLine ("Please enter your email address");
String yx = Han.you3 (Console. ReadLine ());
Console.WriteLine (YX);
Console.ReadLine ();
Console.WriteLine ("Please enter your email address");
Han.you4 (Console. ReadLine ());
Enter the class number and enter each person's score according to the number of people
Since this class is a minority, we need to add 10 points.
A procedure that adds 10 points requires a function
Console.Write ("Please enter class Number:");
int a = Int. Parse (Console.ReadLine ());
Double [] Score =new double [A];
for (int i = 0; i < A; i++)
{
Console.Write ("Please enter the score for {0} individual:", i+1);
Score[i] = double. Parse (Console.ReadLine ());
}
Console.WriteLine ("All students score input completed, please press ENTER to continue!") ");
Console.ReadLine ();
Initialization
Program Hanshu = new program ();
Score = Hanshu. Jiafen (score);
foreach (double AA in Score)
{
Console.WriteLine (AA);
}
Console.ReadLine ();
Scissors
Man-Machine battle
Scissors 1
Stone 2
Baggage 3
Person Input: (scissors, stone, cloth)
0 draw
1 won.
-1 lost.
-2 won.
2 lost.
Console.Write ("Please enter what you punch (scissors, stone, cloth):");
String shu = Console.ReadLine ();
if (Shu = = "Scissors" | | shu = = "Stone" | | shu = = "cloth")
{
int ren = 0;
Switch (SHU)
{
Case "Scissors":
ren = 1;
Break
Case "stone":
ren = 2;
Break
Case "cloth":
ren = 3;
Break
}
Random ran = new random ();
int dian = ran. Next (1, 4);
Switch (DIAN)
{
Case 1:
Console.WriteLine ("Computer Out Scissors");
Break
Case 2:
Console.WriteLine ("Computer Out of stone");
Break
Case 3:
Console.WriteLine ("Computer Out of cloth");
Break
}
int jie = Ren-dian;
if (Jie = = 0)
{
Console.WriteLine ("Draw in this round! ");
}
else if (Jie = = 1 | | jie = =-2)
{
Console.WriteLine ("This round wins!") ");
}
Else
{
Console.WriteLine ("This round fails! ");
}
}
Else
{
Console.WriteLine ("Wrong input! ");
}
Console.ReadLine ();
If an error occurs during the input process
for (int i = 1; i > 0; i++)
{
Console.Write ("Please enter Yes or no!") ");
string ss = Console.ReadLine ();
if (ss = = "Yes" | | ss = = "Not")
{
Break
}
Else
{
Console.WriteLine ("Wrong input! Please re-enter! ");
}
}
Enter month and day to see if the time and date format is correct
for (int i = 1; i > 0; i++)
{
Console.Write ("Please enter the Year:");
int year = Int. Parse (Console.ReadLine ());
if (year >= 0 && year <= 9999)
{
for (int a = 1; a > 0; a++)
{
Console.Write ("Please enter the month:");
int month = Int. Parse (Console.ReadLine ());
if (month >= 1 && month <= 12)
{
for (int b = 1; b > 0; b++)
{
Console.Write ("Please enter Date:");
int day = Int. Parse (Console.ReadLine ());
if (Day >= 1 && Day <= 31)
{
if (month = = 1 | | month = = 3 | | month = = 5 | | month = = 7 | | month = 8 | | month = = | | month = = 12)
{
Console.WriteLine ("The date you entered is correctly formatted, the date you entered is: {0} years {1} month {2} days", year, month, day);
Break
}
Else
{
if (month = = 4 | | month = = 6 | | month = = 9 | | month = = 11)
{
if (day = = 31)
{
Console.WriteLine ("You entered the wrong date, please re-enter!") ");
Continue
}
Else
{
Console.WriteLine ("The date you entered is correctly formatted, the date you entered is: {0} years {1} month {2} days", year, month, day);
Break
}
}
Else
{
if (Day <= 28)
{
Console.WriteLine ("The date you entered is correctly formatted, the date you entered is: {0} years {1} month {2} days", year, month, day);
Break
}
Else
{
if (year% 4 = = 0 && year%!! = 0 | | year% 400 = = 0)
{
if (day = = 29)
{
Console.WriteLine ("The date you entered is correctly formatted, the date you entered is: {0} years {1} month {2} days", year, month, day);
Break
}
Else
{
Console.WriteLine ("You entered the wrong date, please re-enter!") ");
Continue
}
}
Else
{
Console.WriteLine ("You entered the wrong date, please re-enter!") ");
Continue
}
}
}
}
}
Else
{
Console.WriteLine ("You entered the wrong date, please re-enter!") ");
Continue
}
}
A = -10;//jumps out
}
Else
{
Console.WriteLine ("You entered the month error, please re-enter!") ");
Continue
}
}
}
Else
{
Console.WriteLine ("You entered the year error, please re-enter!") ");
Continue
}
i =-10; Jump
}console.readline ();
8.1 Functions