definition of the function :
A larger program should generally be divided into a number of blocks, each of which is used to implement a specific function. All high-level languages have the concept of subroutines, which implements the function of modules with subroutines.
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
}
Void is a parameter that has no return value and is enclosed in parentheses.
The format of the function:
Ⅰ. No reference, no return
public void Leijia ()
{
Console.Write ("Please enter a positive integer:");//Accumulate sum
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
Console.WriteLine (sum);
Console.ReadLine ();
}
static void Main (string[] args)
{
Program Hanshu = new program ();
Hanshu. Leijia ();
}
Ⅱ. No reference and return
public int LeiJia1 ()
{
Console.Write ("Please enter a positive integer:");//Accumulate sum
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
return sum;
}
static void Main (string[] args)
{
Program Hanshu = new program ();
int sum= Hanshu. LEIJIA2 ();
Console.WriteLine (sum);
Console.ReadLine ();
}
Ⅲ. There is a return of the participants
public int LeiJia2 (int a)
{
Accumulate sum
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
return sum;
}
static void Main (string[] args)
{
Program Hanshu = new program ();
int sum= Hanshu. LEIJIA2 (5);
Console.WriteLine (sum);
Console.ReadLine ();
}
Ⅳ. 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 ();
}
static void Main (string[] args)
{
Program Hanshu = new program ();
int sum= Hanshu. LEIJIA3 (5);
}
Attention:
There are parameters that do not need to be received in the function body, there is a return value, I need to use this result in the following, I need to define a variable receive of the same data type when calling the function, function can be nested, but the function can not be nested write.
Practice:
1.
Requirements: Write a function to calculate whether the weight is standard
function requires three input values, gender, weight kg, height cm, respectively
Male: Height -100= weight ±3kg
Female: Height -110= weight ±3kg
public string SDF ()//IS in Format 2: No parameter is returned
{
String o = "You are the standard weight";
String m = "Your weight is too fat";
string n = "Your weight is thin";
String v = "You entered an error!" ";
Console.Write ("Please enter your gender:");
String Qaz = Console.ReadLine ();
Console.Write ("Please enter your weight kg:");
Double wsx = Double. Parse (Console.ReadLine ());
Console.Write ("Please enter your height cm:");
Double EDC = Double. Parse (Console.ReadLine ());
if (Qaz = = "Male")
{
Double A = Wsx-edc + 100;
if (a <= 3 && a >=-3)
{
return o;
}
else if (a > 3)
{
return m;
}
Else
{
return n;
}
}
else if (Qaz = = "female")
{
Double A = Wsx-edc + 110;
if (a <= 3 && a >=-3)
{
return o;
}
else if (a > 3)
{
return m;
}
Else
{
return n;
}
}
Else
{
return v;
}
}
static void Main (string[] args)
{
Program Hanshu = new program ();
String k = Hanshu.sdf ();
Console.WriteLine (k);
Console.ReadLine ();
2. Determine if the mailbox format is correct.
public string Awe (string g)// with reference to return
{
String k = "You have entered the wrong email account!" ";
String h = "The mailbox you entered is" + g;
if (G.contains ("@"))
{
int a = G.indexof ("@");
int B = G.lastindexof ("@");
string w = g.substring (a);
if (a = = b)
{
BOOL C = g.startswith ("@");
if (c = = False)
{
BOOL D = G.endswith (".");
if (d = = false)
{
if (W.contains ("."))
{
String e = G.substring ((A + 1), 1);
string f = g.substring ((A-1), 1);
if (E! = "." && f! = ".")
{
return h;
}
Else
{
return k;
}
}
Else
{
return k;
}
}
Else
{
return k;
}
}
Else
{
return k;
}
}
Else
{
return k;
}
}
Else
{
return k;
}
}
static void Main (string[] args)
{
Program Hanshu = new program ();
Console.Write ("Please enter your email account:");
string s = Hanshu.awe (Console.ReadLine ());
Console.WriteLine (s);
Console.ReadLine ();
}
3. Scissors: Man-machine vs.
Scissors 1 Stone 2 baggage 3
Person Input: (scissors, stone, cloth)
< /em> 0 draw 1 won -1 lost -2 Won 2 lost
public void ess ()//No reference, no return
{
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 ();
}
static void Main (string[] args)
{
Program Hanshu=new program ();
Hanshu.ess ();
}
4. Lottery 3d
public string Qwe ()
{
String o= "Congratulations, you have won a direct election award of 1024 yuan. ";
String p= "Congratulations, you have won 346 yuan in the 3 awards. ";
String t= "Congratulations, you have won 173 yuan in the 6 awards. ";
String y= "I'm sorry you didn't win. ";
String v = "The number you entered is wrong!" ";
int[] qwe = new Int[3];
Random ran = new random ();
Qwe[0] = ran. Next (0, 10);
QWE[1] = ran. Next (0, 10);
QWE[2] = ran. Next (0, 10);
for (int i = 0; i <; i++)
{
Console.clear ();
int a = ran. Next (0, 10);
int B = ran. Next (0, 10);
int C = ran. Next (0, 10);
Console.Write ("Lottery Number:" + A + "+" + B + "+" + C);
System.Threading.Thread.Sleep (100);
}
Console.clear ();
Console.Write ("Lottery number:" + qwe[0] + "+" + qwe[1] + "+" + qwe[2]);
Console.ReadLine ();
Console.Write ("Please enter the number you purchased:");
string s = Console.ReadLine ();
int d = s.length;
if (d = = 3)
{
String e = s.substring (0, 1);
string f = s.substring (1, 1);
String g = s.substring (2, 1);
if (Qwe[0]. ToString () = = e && qwe[1]. ToString () = = f && qwe[2]. ToString () = = g)
{
return o;
}
Else
{
BOOL J = s.contains (Qwe[0]. ToString ());
bool h = s.contains (qwe[1]. ToString ());
BOOL k = S.contains (qwe[2]. ToString ());
if (j = = True && h = = True && k = = True)
{
return p;
}
else if (j = = True && h = = True | | j = True && K = true | | h = true && k = = True)
{
return t;
}
Else
{
return y;
}
}
}
Else
{
return v;
}
}
static void Main (string[] args)
{
Program Hanshu = new program ();
string s = hanshu.qwe ();
Console.WriteLine (s);
Console.ReadLine ();
}
7.31 functions