1. 2. 3. 4. CHAPTER 2-1 2-2 2-3 2-4 VC# 2-5 2-6 2-7 2-8
Visual C# 2008 2-1 Visual C# 0~100 (-32768~+32767) 2 4 VC# (Overflow) 2-1 2-2
2-1 2-1.1 2-1 1 10 10!(1 10) 2-3
Visual C# 2008 10! 32767 short( ) int( ) 2-2 ch2-1_1.sln 2-2 01 03 04 05 06 07 08 09 10 private void Form1_Load(object sender, EventArgs e) { // int t; int total=1; //( short int) for (t = 1; t <= 10; t++) total = total * t; MessageBox.Show("10!=" + total.tostring() ); 2-1 short( 短整數 ) System.Int16 2Byte -32,768 至 32,767 int( 整數 ) System.Int32 4Byte -2,147,483,648 至 2,147,483,647 long( 長整數 ) System.Int64 8Byte -9,223,372,036,854,775,808 至 9,223,372,036,854,775,807 ushort( 無號短整數 ) System.Int16 2Byte 0 至 65,535 uint( 無號整數 ) System.Int32 4Byte 0 至 4,294,967,295 2-4
ulong( 無號長整數 ) System.Int64 8Byte 0 至 18,446,744,073,709,551,615 float( 浮點數 ) System.Single 4Byte 1.5 10-45 至 3.4 10 37 double( 雙精準度 ) System.Double 8Byte 5.0 10-324 至 1.7 10 308 decimal ( 貨幣 ) System.Decimal 16Byte 1.0 10-28 至 7.9 10 28 int a=10; System.Int32 a=10; 2-1.2 string( ) int 2-2 2-2 char( ) System.Char 2Bytes 0 65535 string( ) System.String User 1 2 31 string char c= 'A'; // string s = "A"; // 2-5
Visual C# 2008 2-3 Stu_Name=TextBox1.Text; Eng_Score = int.parse (TextBox3.Text); Chi_Score = int.parse (TextBox2.Text); 2-3 (int.parse ) ch2-1_2.sln 01 03 04 05 06 07 08 09 10 11 12 13 private void Button1_Click(object sender, EventArgs e) { string Stu_Name; int Chi_Score, Eng_Score, Average; Stu_Name = TextBox1.Text; Chi_Score = int.parse (TextBox2.Text); Eng_Score = int.parse (TextBox3.Text); Average = (Chi_Score + Eng_Score) / 2; MessageBox.Show(Stu_Name + " " + Average); 2-6
03~05 static void Main(string[] args) { // int a, b, c; char x, y; float z; int a, b, c; 3*4 byte=12 byte char x, y; 2*2 byte=4 byte float z; 1*4 byte=4 byte ' Backspace Enter escape sequence 2-3 2-3 '\n' (new line) string Str1 = "Visual"; string Str2 = "C#"; MessageBox.Show(Str1+ "\n" + Str2); 2-7
Visual C# 2008 \t (tab) string Str1 = "Visual"; string Str2 = "C#"; MessageBox.Show(Str1+ "\t" + Str2); \r (carriage return) string Str1 = " "; string Str2 = " "; MessageBox.Show(Str1+ "\r" + Str2); \\ string Str1 = "C:"; string Str2 = "Data"; MessageBox.Show(Str1+ "\\" + Str2); \ ' (single quote) string Str1 = "I"; string Str2 = "m a Ph.D. student"; MessageBox.Show(Str1+ "\'" + Str2); \ ' ' (double quote) string Str1 = "I\'m a "; string Str2 = "Ph.D.\" student"; MessageBox.Show(Str1+ "\"" + Str2); \x 16 ASCII string Str1 = " "; string Str2 = " "; MessageBox.Show(Str1+ "\x41" + Str2); 2-1.3 2-4 2-8
2-4 bool( 布林 ) System.Boolean 2Byte True(-1) 或 False(0) object( 物件 ) System.Object 8Byte (Boolean) True( ) False( ) 2-4 if(2>1) printf ("True"); else printf ("False"); 執行結果 2-4 2-2 (Variable) (Constant) A=B+1 1 A B 1 A B C# 2-2.2 2-9
Visual C# 2008 2-2.1 (Variable) 1 int A,B,C; A,B,C 2-5 A B C 2-5 2 2-6 2-10 2-6
VC# 2008 2-2.2 VC# 2008 1 =, 2 =, ; int R; float A; int A,B,C; // R // A // A,B,C 2-11
Visual C# 2008 1+2+ +10 ch2-2_2.sln 01 03 04 05 06 07 08 09 private void button1_click(object sender, EventArgs e) { int i, Sum; for (i = 1; i <= 10; i++) Sum = Sun + 1; MessageBox.Show(" " + Sum); VB ( ) 1 Sum = Sun + 1 Sum = Sum + 1 VC# ( ) Sun 2-12
2-2.3? (=) 01 int R; // 將變數 R 宣告為整數型態 float A; // 將變數 A 宣告為浮點數型態 03 R=5 // 指定 5 給 R 04 A=6.28 6.28 A 01 int R=5; float A=6.28; R 5 R A 6.28 A 2-2.4 Visual C# (Statement) ( ) a = 10; 2-13
Visual C# 2008 01 03 04 05 06 07 { int a, b, c; a = 10; b = 5; c = a + b; MessageBox.Show(c.ToString()); 2-2.5 Visual C# 1. (!, @, #, $, %, * ) 2-5 2. VC# 2008 1 3. 4. (a~z ) 5. ( ) 2-5 M2 正確 ( 英文字母開頭 ) 3M A*5 MessageBox MessageBox 是保留字 2-14
1 (Reserved Word) Visual C#2008 (Property) (Event) (Method) (Operator) (Function) for, loop, if 2-2.6 1. VC# 2008 TOTAL, Total total 2. VC# 2008 Stu_Name( ) Stu_No( ) 3. int Start_X; // X 2-2.7 (Constant) 3.14 3.14 3.14 3.141592 3.14 3.141592 2-15
Visual C# 2008 R = 3; A = 3.14 * (Math.Pow(R,2)); L = 2 * 3.14 * R; R = 3; A = 3.141593 * (Math.Pow(R,2)); L = 2 * 3.14 * R; ( 3.141592 3) ( ) const = ; const double PI=3.141592; // PI 3.141592 01 03 04 05 06 double R, A, L; const double PI=3.141592; //.14 R = 3; A = PI * (Math.Pow(R,2)); L = 2 * PI * R; MessageBox.Show(A.ToString()); PI Visual C# " const string MyName=" "; const string MyID="A123456789"; 2-16
1. 2. 3. 2-3 (60 61 ) 60 60.5 (Implicit Conversion) (Explicit Conversion) 2-3.1 (Implicit Conversion) ( int double) 2-17
Visual C# 2008 ch2-3_1.sln 01 03 04 05 06 07 08 09 10 11 12 13 14 15 16 namespace ch2_3_1 { class Program { static void Main(string args) { int score = 60; double c; c = score; Console.WriteLine(" {0", c); Console.ReadLine(); 2-18
2-3.2 (Explicit Conversion) ( double int) ( int float) int float ( double int) ch2-3_2.sln 2-19
Visual C# 2008 01 03 04 05 06 07 08 09 10 static void Main(string[ ] args) { double score = 60.5; int c; c =(int)score; Console.WriteLine(" {0", c); Console.ReadLine(); (int) (c) ( int float) ch2-3_2a.sln 2-20
01 03 04 05 06 07 08 09 10 static void Main(string[ ] args) { int i = 60, j = 61; float c; c = (i + j) / 2; Console.WriteLine(" {0", c); Console.ReadLine(); ch2-3_2b.sln 01 03 04 05 06 07 08 09 10 static void Main(string args) { int i = 60, j = 61; float c; c = (float)(i + j) / 2; Console.WriteLine(" {0", c); Console.ReadLine(); (float) (c) 2-21
Visual C# 2008 1 c 01 03 04 05 06 07 08 09 10 static void Main(string[ ] args) { int a=10,b=3; double c; c = a/b; Console.WriteLine("c={0",c); Console.ReadLine(); c=3 a/b int( 3) c c = a / b; double int int int 2 c 01 03 04 05 06 07 08 09 10 static void Main(string[ ] args) { int a=10,b=3; double c; c = (double)a / (double)b; Console.WriteLine("c={0",c); Console.ReadLine(); 2-22
c=3.33333333333333 a/b double c c =( double ) a / ( double ) b; double double double double 2-4 VC# /* */ 1. double R, A, L; R,A,L 2-23
Visual C# 2008 2. /* */ /* PI*R2 2*PI*R */ /* */ ch2-4.sln 01 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 private void button1_click(object sender, EventArgs e) { /* PI*R2 *PI*R */ double R, A, L; R,A,L const double PI = 3.141592; //.14 R = 3; A = PI * (Math.Pow(R, 2)); L = 2 * PI * R; MessageBox.Show(" =" + A.ToString()); MessageBox.Show(" =" + L.ToString()); 2-24
2-5 C# ➊ int i, j; int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { // ➋ for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { // 2-6 --- --- (Expression) (Operand) (Operator) A=B+1 A,B,1 =,+ 2-25
Visual C# 2008 ( ) ( ) ( ) ( ) 2-7 A = PI * (Math.Pow(R, 2)); L = 2 * PI * R; = + + 2-7 2-6.1 VC# 2008 = (* 1) = A B Sum 10 Sum=0; 20 A=1; 30 B=2; 40 Sum=A+B; 1 VB = Sum 0 3 2-26
= 10 Sum=0; ( ) 20 A=1; ( ) 30 2=B; ( ) = 40 A+B=Sum; ( ) = 30 40 2-6.2 2-6 2-6 1 ++ ( ) -- ( ) a 1 a 1 a++ a-- 2 ( ) a b a*b 30 ( a=10,b=3) a= a +1 a = a -1 2 ( ) a b a/b 3.33333333. 2 % ( ) a b a % b 1 3 ( ) a b a+b 13 3 ( ) a b a-b 7 1 * 2-27
Visual C# 2008 2 (++) (--) (1) ++i ++ -- 1 1 A=10; B=++A; A=10; B=--A; A=11 B=11 A 1 B A=9 B=9 A 1 B (2) i++ ++ -- 1 1 A=10; B=A++; A=10; B=A--; A=11 B=10 A B A 1 A=9 B=10 A B A 1 3 %( ) a b a b VB Mod 2-28
1 ➊? (3+5)*2 ➋ Enter ➌ (3+5)*2 16 ➋ 再按 Enter ➊ 首先輸入? ➌ 系統會即時在下一行顯示結果 2-8 2 6+15/3*2-4 ➊ 15/3=5 ➋ 5*2=10 ➌ 6+10=16 ➍ 16-4=12 (6+15)/3*2-4 ➊ 6+15=21 ➋ 21/3=7 7*2=14 ➌ 14-4=10 2-29
Visual C# 2008 VC# (1) F=3A+2B+C (2) F= A X 2 + B X + C (3) F=( A) X Y + B Z +1 (4) F=( A + B)(C +D) (5) F= A X 2.5 + B X 1.5 + C X 0.5 (1) F=3*A+2*B+C (2) F=A* Math.Pow(X,2)+ Math.Pow(B,X)+C (3) F= Math.Pow(A,X*Y)+ Math.Pow(B,Z)+1 (4) F=(A+B)*(C+D) (5) F=A*Math.Pow(X,2.5)+B* Math.Pow(X,1.5)+C* Math.Pow(X,0.5) a,b 01 03 04 05 06 07 static void Main(string[ ] args) { int i=10; int a, b; a = i++; b = i--; ➊ a=10 ( i a 1) ➋ b=11 ( 05 i 11 06 i b 1) 2-30
a,b 01 03 04 05 06 07 static void Main(string[ ] args) { int i=10; int a, b; a = i++; b = ++i; (++) (--) ➊ a=10 ( i a 1) ➋ b=12 ( 05 i 11 06 ++ i i 1(i=12) b) 2-6.3, 1. True( ) False( ) 2-7 2-31