Sunteți pe pagina 1din 7

Programming Language 1

using
Visual C# Visual C#
Lesson 08
Predefined Methods
Prepared by: Mr. Dan Chua for SISC
Predefined Methods
1. Write( ) Method allows you to display text information on the
screen by maintaining a single line
ex. Console.Write("This is a Text ");
Console.Write(506);
Console.Write(True);
Console.Write(S)
// Output: This is a Text 506TrueS
2. WriteLine( ) Method allows you to display text information on the
screen by advancing one line after executing the screen by advancing one line after executing the
statement.
ex. Console.WriteLine("This is aText ");
Console.WriteLine({0:c,506);
Console.WriteLine(True);
Console.WriteLine(S)
// Output: This is a Text
$506.00
True
S
Predefined Methods
3. Read( ) Method It holds the output window open until a character from the
keyboard was pressed. This enabled the user of your
program to leave the output on the screen as long as
necessary.
ex. Console.Read();
Returns an int representing the next character input from
the keyboard.
ex. int key;
key = Console.Read(); // input: A
Console.WriteLine(key) // output: 65 (ASCII value for A Console.WriteLine(key) // output: 65 (ASCII value for A
4. ReadLine( ) Method Returns all characters up to the current end-of-line
terminator, the Enter key.
It always returns a string. Even if numbers are entered, a string
representing the concatenation of these numbers is returned.
ex. int key, n;
bool b;
key = Convert.ToInt32(Console.ReadLine());
b = int.TryParse(Console.ReadLine(), out n);
Console.WriteLine(key);
Methods in MathClass
Methods Description Example
Abs
Returns the absolute value of a
specified number
Console.WriteLine(Math.Abs(num1));
// 87.34
Ceiling
Returns the smallest whole number
greater than or equal to the specified
ans = (int)Math.Ceiling(num1);
// -87
Each of the following methods is static, calling them must include the Math class name as qualifier.
Example:
float num1 = -87.34F;
double num2 = 38.12;
int ans;
Ceiling
greater than or equal to the specified
number
// -87
ans = (int)Math.Ceiling(num2);
// 39
Floor
Returns the largest whole number less
than or equal to the specified number
ans = (int)Math.Floor(num1);
// -88
ans = (int)Math.Floor(num2);
// 38
Max
Returns the larger of two specified
numbers
ans = Math.Max(89,90)
// 90
Min
Returns the smaller of two specified
numbers
ans = Math.Min(89,90)
// 89
Methods in MathClass
Methods Description Example
Pow
Returns a specified number raised to
the specified power
ans = Math.Pow(num1,3);
// 64
Round
Returns the number nearest the Math.Round(num2,5)
Each of the following methods is static, calling them must include the Math class name as qualifier.
Example:
int num1 = 4;
float num2 = 54.90293857f;
int ans;
Round
Returns the number nearest the
specified value
Math.Round(num2,5)
// 54.90294
Sign
Returns a value indicating the sign of a
number
-1 for negative value
1 for positive value
0 for zero value
ans = Math.Sign(-34); // -1
ans = Math.Sign(89) ; // 1
ans = Math.Sign(0) // 0
Sqrt
Returns the square root of a specified
number
ans = (int)Math.Sqrt(36));
// 6
Machine Problem #04 See Moodle for details
End of Lesson 08
Predefined Methods

S-ar putea să vă placă și