Main static void Mainstring args const double pi
ภายในเมธอด Main • การประกาศตวแปร • คำสง static void Main(string[] args) { const double pi = 3. 1416; int radius; double area; radius = int. Parse(Console. Read. Line()); area = pi*radius; Console. Write. Line(area); }
Type bool char byte short int long แบบชนดขอมลพนฐานใน ทบทวน Size 1 byte 2 byte 4 byte 8 byte Description Store truth value Store one character Store positive integer Store integer double 16 byte Store real number string N/A Store sequence of characters C#: Range true / false character code 0 – 255 -32, 768 -- 32, 767 -2. 1 x 109 -- 2. 1 x 109 -9. 2 x 1018 -- 9. 2 x 1018 ± 5. 0 x 10 -324 -± 1. 7 x 10308 N/A
������ Console. Write. Line • �������� Console. Write. Line("Hello"); Console. Write. Line(area); • ����������� Console. Write. Line(”Size {0}x{1}”, width, height); • ����������� double salary=12000; Console. Write. Line("My salary is {0: f 2}. ", salary); 20
เฉลยมมนกคด public static void Main(string[] args) { Console. Write("Enter weight (in kg): "); double w = double. Parse(Console. Read. Line()); Console. Write("Enter height (in m): "); double h = double. Parse(Console. Read. Line()); Console. Write. Line("Your BMI is {0}. ", w / (h*h)); (กดเพอแสดง Console. Read. Line(); } จดรปแบบไมสวย Enter weight (in kg): 83 Enter height (in m): 1. 7 Your BMI is 28. 719723183391. (
เฉลยมมนกคด 1 (ครงทาง ( • แสดงเฉพาะสวนเมธอด Main static void Main() { Console. Write("Enter m (m): "); double m = double. Parse(Console. Read. Line()); Console. Write("Enter v (m/s): "); double v = double. Parse(Console. Read. Line()); Console. Write("Enter t (min): "); double t = double. Parse(Console. Read. Line()); (กดเพอแสดง double dist = v * t * 60; double walk = dist / (2*m); Console. Write. Line("Number of times = {0}", walk); Console. Read. Line(); } (
เฉลยมมนกคด 1 • แสดงเฉพาะสวนเมธอด Main static void Main() { Console. Write("Enter m (m): "); double m = double. Parse(Console. Read. Line()); Console. Write("Enter v (m/s): "); double v = double. Parse(Console. Read. Line()); Console. Write("Enter t (min): "); double t = double. Parse(Console. Read. Line()); (กดเพอแสดง double dist = v * t * 60; int walk = (int)(dist / (2*m)); Console. Write. Line("Number of times = {0}", walk); Console. Read. Line(); } (
ตวอยาง : ไลพมพคา int a = 1; while(a <= 10) { Console. Write. Line(a); a = a + 1; } } int a = 1; while(a <= 10) { Console. Write. Line(a); a++; } a += 1;
รายการเมธอดทางคณตศาสตร Method/ Constant Value returned Example Call Result PI Value of Math. PI 3. 1415927 Max(x, y) Larger of the two Math. Max(1, 2) 2 Abs(x) Absolute value of x Math. Abs(-1. 3) 1. 3 Sqrt(x) Square-root of x Math. Sqrt(4. 0) 2. 0 Round(x) Nearest integer to x Math. Round(0. 8) 1 Pow(x, y) xy Math. Pow(3, 2) 9. 0 Log(x) Natural log of x Math. Log(10) 2. 302585 Ceiling(x) Smallest integer greater Math. Ceiling(4. 1) than or equal to x 5 Cos(x) Cosine of x radians -1 Math. Cos(Math. PI)
เฉลย 1: if ธรรมดา if(bmi < 18. 5) Console. Write. Line("Underweight"); if((bmi >= 18. 5) && (bmi < 25)) Console. Write. Line("Normal"); if((bmi >= 25) && (bmi < 30)) Console. Write. Line("Overweight"); (กดเพอแสดง if(bmi >= 30) Console. Write. Line("Extremely fat"); (
เฉลย 2: if ซอนกน if(bmi < 18. 5) Console. Write. Line("Underweight"); else if(bmi < 25) Console. Write. Line("Normal"); else (กดเพอแสดง if(bmi < 30) Console. Write. Line("Overweight"); else Console. Write. Line("Extremely fat"); (
การจบค else กบ if if(a > 5) if(b < 10) Console. Write. Line("Hello"); else Console. Write. Line("Good-bye"); if(a > 5) if(b < 10) Console. Write. Line("Hello"); else Console. Write. Line("Good-bye"); ในกรณทไมมการระบอยางชดเจน else จะจบคกบ if ทอยใกลกวาเสมอ
ตวอยาง : หกภาษ • ในการหกลดหยอนภาษ ถามบตร สามารถหกลดหยอนไดคนละ 15, 000 บาท แตหกไดไมเกน 3 คน • ตองการเขยนโปรแกรมดงดานลาง Do you have any children (Y/N)? N Your deduction is 0 baht. Do you have any children (Y/N)? Y How many children do you have? 2 Your deduction is 30000 baht Do you have any children (Y/N)? Y How many children do you have? 5 Your deduction is 45000 baht
ลดหยอน : ขนแรก public static void Main(string[] args) { Console. Write("Do you have any children (Y/N)? "); string ans = Console. Read. Line(); int deduction = 0; if(ans == "Y") { // ������������ } else deduction = 0; Console. Write. Line("Your deduction is {0} baht. ", deduction); Console. Read. Line(); }
ลดหยอน : สมบรณ public static void Main(string[] args) { Console. Write("Do you have any children (Y/N)? "); string ans = Console. Read. Line(); int deduction = 0; if((ans == "Y") || (ans == "y")) { Console. Write("How many children do you have? "); int ccount = int. Parse(Console. Read. Line()); if(ccount > 3) ccount = 3; deduction = ccount * 15000; } else deduction = 0; Console. Write. Line("Your deduction is {0} baht. ", deduction); Console. Read. Line(); }
คำสง • คำสง do - while ไมมในภาษาไพธอน do-while statement 1 statement 2 do คำสง while( เงอนไข : ); statement n true condition false 64
ตวอยาง • รบรหสผาน "hellocsharp" เขยนดวย while เขยนดวย dowhile จนกวาผใชจะพมพ string pwd; pwd = Console. Read. Line(); while(pwd != "hellocsharp") pwd = Console. Read. Line(); string pwd; do pwd = Console. Read. Line(); while(pwd != "hellocsharp"); นสตคดวาแบบใดเขาใจไดงายกวา ?
ตวอยาง • รบจำนวนเตม n พมพเลข 1 – n int n = int. Parse( บรรทดละหนงตว เขยนดวย while int n = int. Parse( Console. Read. Line()); int i = 1; do { Console. Write. Line(i); i++; } เขยนดวย while(i <= n); while Console. Read. Line()); int i = 1; while(i <= n) { Console. Write. Line(i); i++; } do-
- Slides: 74