Case 7:
Class Program
{
Static Void Main ( String [] ARGs)
{
Account account1 = New Account ( 50.00 m );
Account Account2 = New Account (- 7.00 m );
Console. writeline ( " {0: c} " , Account1.balance ); // 50
Console. writeline ( " {0: c} " , Account2.balance );
}
}
Class Account
{
// Variable prefix _, attribute prefix in upper case
Private Decimal _ Balance; // Private variable
Public Account ( Decimal I) // Constructor
{
Balance = I; // It is easy to make mistakes here. If you write balance in lower case, the following if will be useless...
}
Public Void Credit ( Decimal Amount) // No Return Value Method
{
_ Balance + = amount;
}
Public Decimal Balance // Attribute
{
Get {Return _ Balance ;}
Set
{
If (Value> = 0 )
{
This . _ Balance = value;
}
}
}
}
Case 8:
Class Program
{
Static Void Main ( String [] ARGs) // Main Function
{
Grade One = New Grade ( " Mr. w " ); // Create an object and let the variable one reference the object
One. classaverge (); // Call Method
}
}
Class Grade
{
Public Grade ( String Name) // Constructor
{
Console. writeline ( " Welcome for {0} " , Name );
}
Public Void Classaverge ()
{
Int Total = 0 ; // Total
Int Count = 0 ; // Count
Double Averge = 0 ;// Average
Int Number; // Score
For ( Int I = 0 ; I <= 10 ; I ++)
{
Total = 0 ;
Console. writeline (" Please enter number: " );
Number = convert. toint32 (console. Readline ());
Total + = number;
Count = I + 1 ;
}
Averge = total/count;
Console. writeline ( " Total is {0} " , Total );
Console. writeline (" Count is {0} " , Count );
Console. writeline ( " Average is {0} " , Averge );
}
}
Case 9:
Note: The reason for writing this case is that I % 2 = 0 was previously calculated, and now it is self-incrementing 2.
Static Void Main ( String [] ARGs) // Main Function
{
// Returns the sum of the two-20 even numbers.
Int Total = 0 ;
For ( Int I = 2 ; I <= 20 ; I + = 2 )
{
Total + = I;
Console. writeline (I +" \ T " + Total );
}
Console. writeline (total );
}
Case 11:
Class Program
{
Static Void Main ( String [] ARGs) // Main Function
{
Findmax max =New Findmax (); // Call methods using objects
Max. Maxi ();
Console. writeline (findmax. maxnum ( 10 , 50 , 60 )); // Directly use static functions
}
}
Class Findmax // Find the maximum number of classes
{
Public Void Maxi () // A method without a return value. This method calls an internal method.
{
Double Num1;
Double Num2;
Double Num3;
Console. writeline ( " Please enter 1: " );
Num1 = convert. todouble (console. Readline ());
Console. writeline ( " Please enter 2: " );
Num2 = convert. todouble (console. Readline ());
Console. writeline ( " Please enter 3: " );
Num3 = convert. todouble (console. Readline ());
// Double result = maxnum (num1, num2, num3 );
// Console. writeline ("Max number is {0}", result); two writing methods
Console. writeline ( " This is having class: " + Findmax. maxnum (num1, num2, num3 )); // This is called directly in the class.
Console. writeline ( " This is no class: " + Maxnum (num1, num2, num3 )); // You do not need to use the class name + because it is in the same class.
}
Public Static Double Maxnum ( Double A, Double B, Double C) // Set static functions
{
Double Maxnum =;
If (B> = maxnum)
{
Maxnum = B;
}
If (C> = maxnum)
{
Maxnum = C;
}
Return Maxnum;
}
}
Case 12:
Math class method:
Absolute Value of math. Abs (x) x
Math. Ceiling (X) is not smaller than the minimum integer of X
Cosine of math. Cos (x) x
Math. exp (x) exponential function
Math. Floor (X) is not greater than the maximum integer of X
Math. Max (x, y) outputs the maximum value // three functions cannot be used
Math. Min (x, y) Output minimum value
Math. Pi constant π
Note: For methods with no return value for void, to output information, you must console it yourself in the method!
Case 13:
Class Program
{
Static Void Main ( String [] ARGs)
{
Refandout. romethod (); // When a method is static, objects are not required for methods that reference classes.
}
}
Class Refandout
{
Public Static Void Romethod () // If there are no parentheses, attribute is used. If the public method in the class is static, all methods called by this public method must be STIC.
{
Int Y = 5 ;
Int Z;
Console. writeline ( " The first time, Y is {0} " , Y );
Console. writeline ( " The first time, z is {0} " , " \ "Z is unintitialized \" " );
// In fact, ref out can form overload functions.
Squareref ( Ref Y );
Squareout ( Out Z );
Console. writeline (y ); // 25
Console. writeline (z ); // 36
Square (y );
Square (z );
Console. writeline (y ); // 25 No ref, and the original value of out is not changed
Console. writeline (z );// 36
}
Static Void Squareref ( Ref Int X)
{
X = x * X;
}
Static Void Squareout ( Out Int X)
{
X = 6 ;
X = x * X;
}
Static Void Square ( Int X) // Although this is square calculation, it is void, and the original value cannot be changed without returning the value.
{
X = x * X;
}
}