C 01204111 Computer and Programming C using System

  • Slides: 74
Download presentation
แนะนำภาษา C# 01204111 Computer and Programming

แนะนำภาษา C# 01204111 Computer and Programming

โปรแกรมแรกบน C# using System; โปรแกรมน ทำอะไร ? class Program { public static void Main(string[]

โปรแกรมแรกบน C# using System; โปรแกรมน ทำอะไร ? class Program { public static void Main(string[] args) { Random r = new Random(); int answer = 1 + (r. Next() % 100); int g; do { Console. Write("Guess a number: "); g = int. Parse(Console. Read. Line()); if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); } }

using System; ผาตดโปรแกรม class Program { public static void Main(string[] args) { Random r

using System; ผาตดโปรแกรม class Program { public static void Main(string[] args) { Random r = new Random(); int answer = 1 + (r. Next() % 100); int g; do { Console. Write("Guess a number: "); g = int. Parse(Console. Read. Line()); if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); } } โปรแกรมหลกอย ในฟงกชน )เมทอด ) ชอ Main

using System; ผาตดโปรแกรม class Program { public static void Main(string[] args) { Random r

using System; ผาตดโปรแกรม class Program { public static void Main(string[] args) { Random r = new Random(); int answer = 1 + (r. Next() % 100); int g; do { Console. Write("Guess a number: "); g = int. Parse(Console. Read. Line()); if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); } } ใชค { } ในการ เปด -ปดขอบเขต บลอก

ผาตดเมทอด Main public static void Main(string[] args) { Random r = new Random(); int

ผาตดเมทอด Main public static void Main(string[] args) { Random r = new Random(); int answer = 1 + (r. Next() % 100); int g; do { Console. Write("Guess a number: "); สมคำตอบ g = int. Parse(Console. Read. Line()); ระหวาง 1 - 100 if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); }

ผาตดเมทอด Main public static void Main(string[] args) ทำซำไปเรอย { ๆ ถา Random r =

ผาตดเมทอด Main public static void Main(string[] args) ทำซำไปเรอย { ๆ ถา Random r = new Random(); int answer = 1 + (r. Next() % 100); g != answer int g; do { Console. Write("Guess a number: "); g = int. Parse(Console. Read. Line()); if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); }

ผาตดเมทอด Main public static void Main(string[] args)พมพขอความ { และอานคาจำนวนเ Random r = new Random();

ผาตดเมทอด Main public static void Main(string[] args)พมพขอความ { และอานคาจำนวนเ Random r = new Random(); ตม int answer = 1 + (r. Next() % 100); เกบในตวแปร g int g; do { Console. Write("Guess a number: "); g = int. Parse(Console. Read. Line()); if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); }

ผาตดเมทอด Main public static void Main(string[] args) เปรยบเทยบ { g กบ answer Random r

ผาตดเมทอด Main public static void Main(string[] args) เปรยบเทยบ { g กบ answer Random r = new Random(); int answer = 1 + (r. Next() % 100); แลวพมพคำใบ int g; do { Console. Write("Guess a number: "); g = int. Parse(Console. Read. Line()); if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); }

ผาตดเมทอด Main public static void Main(string[] args) { Random r = new Random(); int

ผาตดเมทอด Main public static void Main(string[] args) { Random r = new Random(); int answer = 1 + (r. Next() % 100); int g; do { Console. Write("Guess a number: "); พมพวาตอบถกต g = int. Parse(Console. Read. Line()); อง if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); }

ผาตดโปรแกรม : กรอบภายนอก using System; class Program { public static void Main(string[] args) {

ผาตดโปรแกรม : กรอบภายนอก using System; class Program { public static void Main(string[] args) { Random r = new Random(); int answer = 1 + (r. Next() % 100); int g; do { Console. Write("Guess a number: "); g = int. Parse(Console. Read. Line()); if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); } } ระบใหใชชดค ำสงมาตรฐานใน คลง

using System; ผาตดโปรแกรม class Program { public static void Main(string[] args) { Random r

using System; ผาตดโปรแกรม class Program { public static void Main(string[] args) { Random r = new Random(); int answer = 1 + (r. Next() % 100); int g; do { Console. Write("Guess a number: "); g = int. Parse(Console. Read. Line()); if(g > answer) Console. Write. Line("Too large"); else if(g < answer) Console. Write. Line("Too small"); } while(g != answer); Console. Write. Line("That's correct"); } } ทก ๆ เมทอด จะตองอยในคลา ส (class)

โครงสรางของโปรแกรมภาษา C# using System; namespace Sample { class Program { static void Main() {

โครงสรางของโปรแกรมภาษา C# using System; namespace Sample { class Program { static void Main() { Console. Write. Line("Hello, world"); } } } ���� C# ������ {} ����������� : 1. namespace Sample 2. class Program 3. method Main

จกรวาลของโปรแกรม Namespace System Class X Class Console Write. Line Class Float Write Namespace Sample

จกรวาลของโปรแกรม Namespace System Class X Class Console Write. Line Class Float Write Namespace Sample )เนมสเปสของ โปรแกรมเรา( Read. Line Class Integer Parse To. String Class Random Next Class Y Class Program Main

using System • การสง using System; ในตอนตนโปรแกรมเปนการบอกวาเราจะใชโปรแกรม จากเนมสเปส System เหมอนเปนของเราเอง using System; namespace Sample

using System • การสง using System; ในตอนตนโปรแกรมเปนการบอกวาเราจะใชโปรแกรม จากเนมสเปส System เหมอนเปนของเราเอง using System; namespace Sample { class Program { …… } } Namespace System Class Console Write. Line Write Class Float Read. Line Class Integer Parse To. String Class Random Next Namespace Sample )เนมสเปสขอ งโปรแกรมเรา( Class Program Main

โปรแกรมหลก : เมทอด Main using System; namespace Sample { class Program { static void

โปรแกรมหลก : เมทอด Main using System; namespace Sample { class Program { static void Main() { �������� Main ���������� } } }

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[]

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[] args) { double a, t, s; Console. Write("Enter a: "); a = double. Parse(Console. Read. Line()); Console. Write("Enter t: "); t = double. Parse(Console. Read. Line()); s = a*t*t/2. 0; Console. Write. Line("Distance = {0}", s); } } } ลองสงเกตสงทเห มอนและแตกตางของโ ปรแกรมเมอเทยบก บโปรแกรมภาษาไพธอน

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[]

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[] args) { double a, t, s; Console. Write("Enter a: "); a = double. Parse(Console. Read. Line()); Console. Write("Enter t: "); t = double. Parse(Console. Read. Line()); s = a*t*t/2. 0; Console. Write. Line("Distance = {0}", s); } } } ทก ๆ คำสงตองจบดวย เครองหมาย ; (semi-colon)

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[]

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[] args) { double a, t, s; Console. Write("Enter a: "); a = double. Parse(Console. Read. Line()); Console. Write("Enter t: "); t = double. Parse(Console. Read. Line()); s = a*t*t/2. 0; Console. Write. Line("Distance = {0}", s); } } } กอนใชตวแปรใด ๆ จะตองมการประกาศก อน และตองระบแบบชน ด (type) ขอมลทตวแปรน นเกบดวย ในทน double คอชนดขอมลทเกบ จำนวนจรง

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[]

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[] args) { double a, t, s; Console. Write("Enter a: "); a = double. Parse(Console. Read. Line()); Console. Write("Enter t: "); t = double. Parse(Console. Read. Line()); s = a*t*t/2. 0; Console. Write. Line("Distance = {0}", s); } } } คำนวณผลลพธเกบ ในตวแปร s

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[]

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[] args) { double a, t, s; Console. Write("Enter a: "); a = double. Parse(Console. Read. Line()); Console. Write("Enter t: "); t = double. Parse(Console. Read. Line()); s = a*t*t/2. 0; Console. Write. Line("Distance = {0}", s); } } } คำสงแสดงผล

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[]

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[] args) { double a, t, s; Console. Write("Enter a: "); a = double. Parse(Console. Read. Line()); Console. Write("Enter t: "); t = double. Parse(Console. Read. Line()); s = a*t*t/2. 0; Console. Write. Line("Distance = {0}", s); } } } อานขอมลเปนสตร ง

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[]

using System; โปรแกรมแรก namespace mecha 1 { class Program { public static void Main(string[] args) { double a, t, s; Console. Write("Enter a: "); a = double. Parse(Console. Read. Line()); Console. Write("Enter t: "); t = double. Parse(Console. Read. Line()); s = a*t*t/2. 0; Console. Write. Line("Distance = {0}", s); } } } แปลงสตรง เปนจำนวนจรง (double)

ความแตกตาง : ใช { } ในการระบบลอค C# // //������������ // static void Main() {

ความแตกตาง : ใช { } ในการระบบลอค C# // //������������ // static void Main() { int n = int. Parse(Console. Read. Line()); int n = int i = 1; int. Parse(Console. Read. Line; (() while(i <= n) int i = 1; { while(i <= n ( if(i % 3 == 0) if(i % 3 == 0( { Console. Write. Line(i ; ( Console. Write. Line(i); { } } } ��������� while ���� if ���� {}

ความแตกตาง : ตองประกาศตวแปร Python n = int(input()) total = 0 i = 0 while

ความแตกตาง : ตองประกาศตวแปร Python n = int(input()) total = 0 i = 0 while i < n: x = int(input(() total += x i += 1 print(total( C# // //������������ // static void Main() { int n = int. Parse(Console. Read. Line()); int total = 0; int i = 0; int x; while(i < n) { x = int. Parse(Console. Read. Line()); total += x; i += 1; } }

แบบชนดขอมลพนฐานใน Type bool char byte short int long Size 1 byte 2 byte 4

แบบชนดขอมลพนฐานใน 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

หาผลรวม: Python total = 0 num = 0 while num != -1: num =

หาผลรวม: Python total = 0 num = 0 while num != -1: num = int(input()) if num != -1: total += num print(total)

หาผลรวม: C# • โปรแกรมหาผลรวม ในภาษา C# static void Main() { int total = 0;

หาผลรวม: C# • โปรแกรมหาผลรวม ในภาษา C# static void Main() { int total = 0; int num = 0; while(num != -1) { num = int. Parse(Console. Read. Line()); if(num != -1) total += num; } Console. Write. Line(total); } total = 0 num = 0 while num != -1: num = int(input()) if num != -1: total += num print(total)

ตวอยางโปรแกรมภาษาไพธอน x = int(input("Enter price: ")) if x <= 10: print("You have to pay",

ตวอยางโปรแกรมภาษาไพธอน x = int(input("Enter price: ")) if x <= 10: print("You have to pay", x, "baht. ") else: print("You have to pay", y*0. 95, "baht. ") Enter price: 7 You have to pay 7 baht. เหนทผดหรอไม เราจะพบทผดลกษณะน ในไพธอน Enter price: 20 เมอตอนโปรแกรมทำงานเทาน Traceback (most recent call last): น File "<string>", line 5, in <fragment> builtins. Name. Error: name 'y' is not defined ?

ตวอยางสวนของโปรแกรมภาษา C# public static void Main(string[] args) { เหนทผดหรอไม double x; Console. Write("Enter price:

ตวอยางสวนของโปรแกรมภาษา C# public static void Main(string[] args) { เหนทผดหรอไม double x; Console. Write("Enter price: "); x = double. Parse(Console. Read. Line()); ? if(x <= 10) Console. Write. Line("You have to pay {0} baht. ", x); else Console. Write. Line("You have to pay {0} baht. ", y*0. 95); } ใน C# โปรแกรมนจะไมผานการตรวจสอบ ระบบจะแจงวา The name 'y' does not exist in the current context.

ตวอยางสวนของโปรแกรมภาษา C# public static void Main(string[] args) { เหนทผดหรอไม String x; Console. Write("Enter price:

ตวอยางสวนของโปรแกรมภาษา C# public static void Main(string[] args) { เหนทผดหรอไม String x; Console. Write("Enter price: "); x = Console. Read. Line(); ? if(x <= 10) Console. Write. Line("You have to pay {0} baht. ", x); else Console. Write. Line("You have to pay {0} baht. ", x*0. 95); } ใน C# โปรแกรมนจะไมผานการตรวจสอบ ระบบจะแจงขอผดพลาด 2 ขอ คอ 1. Operator '<=' cannot be applied to operands of type 'string' and 'int'. 2. Operator '*' cannot be applied to operands of type 'string' and 'double'

สราง solution • เลอก new solution • เลอก Windows Application • พมพชอ solution

สราง solution • เลอก new solution • เลอก Windows Application • พมพชอ solution

จดการกบการกดปม • Double click ทปม จากนนเขยนเมทอดดงน void Add. Button. Click(object sender, Event. Args e)

จดการกบการกดปม • Double click ทปม จากนนเขยนเมทอดดงน void Add. Button. Click(object sender, Event. Args e) { int x = int. Parse(x. Text. Box. Text); int y = int. Parse(y. Text. Box. Text); int result = x + y; result. Label. Text = result. To. String(); }