Using Variable using System class Using Variable static
程式 Using. Variable using System; class Using. Variable { static void Main() { char str. My. String = 'a'; int. My. Int = 123; float f. My. Float; Console. Write. Line( str. My. String ); Console. Write. Line( int. My. Int ); f. My. Float = 123. 456 f; Console. Write. Line( f. My. Float ); Console. Read. Line(); } } 2
Main Memory *J. G. Brookshear, Computer Science – An Overview, 8 th edition, Addison-Wesley, 2005 3
程式 Using. Constant using System; class Using. Constant { static void Main() { int my. Int = 123; const int MY_CONST = 456; Console. Write. Line("變數my. Int = {0} ", Console. Write. Line("常數MY_CONST = {0} MY_CONST); my. Int = 321; Console. Write. Line("變數my. Int = {0} ", Console. Read. Line(); } } my. Int); ", my. Int); 7
資料型別 • 實值型別(Value Type) – 結構(Structs) • • • 數值(Numeric) 布林(bool) 使用者定義Structs – 列舉(Enumerations) • 參考型別(Reference Type) – 字串(string) – 物件(object) 9
程式 Using. Numeric using System; class Using. Numerics { static void Main() { int x = 256; byte y = 255; Console. Write. Line("x Console. Write. Line("y double z = 123. 45; float f = 123. 45 f; Console. Write. Line("z Console. Write. Line("f decimal d = 123. 45 m; Console. Write. Line("d } } : : " " + + x); y); : : " " + + z); f); : " + d); 10
整數型別 • sbyte: -128 ~ 127 • byte: 0 ~ 255 • short: -32768 ~ 32767 • unshort: 0 ~ 65535 • int: -2147483648 ~ 2147483647 • uint: 0 ~ 4294967295 • long: -9223372036854775808 ~ 9223372036854775807 • ulong: 0 ~ 18446744073709551615 • char: U+0000 ~ U+ffff 11
decimal 型別 • 28 ~ 29 位小數, 正負 1. 0 e-28 ~ 7. 9 e 28, 128 位元 13
程式 Using. Char (1/2) using System; class Using. Char { static void Main(string[] args) { char the. Char 1 = 'a'; char the. Char 2 = '文'; char the. Char 3 = 'x 0059'; char the. Char 4 = 'u 0058'; char the. Char 5 = 'n'; char the. Char 6 = '''; Console. Write. Line(the. Char 1); Console. Write. Line(the. Char 2); Console. Write. Line(the. Char 3); 14
程式 Using. Char (2/2) Console. Write. Line(the. Char 4); Console. Write. Line(the. Char 5); Console. Write. Line(the. Char 6); Console. Read. Line(); } } 15
程式 Conversion (1/2) using System; class Conversion { static void Main(string[] args) { int a = 10; double b = 0; b = a; b = 20. 5; a = (int)b; Console. Write. Line("a = " + a); Console. Write. Line("b = " + b); float c = 20; c = 20. 5 f; c = (float)20. 5; 19
程式 Conversion (2/2) Console. Write. Line("c = " + c); char d = (char) 65; Console. Write. Line("d = " + d); Console. Read. Line(); } } 20
變數設定與型別轉換 • 變數設定 (Assignment) • 隱含轉換 (Implicit conversion) • 強制轉換 (Explicit conversion) 21
字串與字元 string. Value 1 = “abc”; string. Value 2 = “a”; char. Value = ‘a’; 22
堆疊(Stack)與堆積(Heap) Heap . . . Stack 23
程式 Using. Math. Operator (1/2) using System; namespace Using. Math. Operator { class Program { static void Main(string[] args) { int x; int y; Console. Write. Line("請輸入第一個整數值x : "); x = int. Parse(Console. Read. Line()); Console. Write. Line("請輸入第二個整數值y : "); y = int. Parse(Console. Read. Line()); Console. Write. Line(" x + y = {0} ", x + y); Console. Write. Line(" x - y = {0} ", x - y); 26
程式 Using. Math. Operator (2/2) Console. Write. Line(" x x x * / % y y y = = = {0} {0} ", ", ", x x x * / % y); y); } } } 27
程式 Using. In. De. Operator (1/3) using System; namespace Using. In. De. Operator { class Program { static void Main(string[] args) { int x 0; int x; int add; Console. Write. Line("請輸入整數變數x初值"); x 0 = int. Parse(Console. Read. Line()); Console. Write. Line("請輸入所要加總的整數值add"); add = int. Parse(Console. Read. Line()); 29
程式 Using. In. De. Operator (2/3) x = x 0; x = x + add; Console. Write. Line( "使用運算式"x = x + add" 運算結果等於{0} x = x 0; x += add; Console. Write. Line( "使用運算式"x += add" 運算結果等於{0} ", int pre; int post; x = x 0; post = x++; x = x 0; pre = ++x; ", x); 30
程式 Using. In. De. Operator (3/3) Console. Write. Line( "使用運算式"post = x++後" post等於{0} ", post); Console. Write. Line( "使用運算式"post = x++後" x等於{0}", x); Console. Write. Line( "使用運算式"pre = ++x" pre等於{0} ", pre); Console. Write. Line( "使用運算式"pre = ++x後" x等於{0}", x); Console. Read. Line(); } } } 31
型別轉換錯誤三例 • 例1 byte b. Value = 254; b. Value = b. Value*2; • 例2 byte b. Value; int aa = 0; b. Value = aa + 0; • 例3 float f = 0; f = 0. 1 + 0. 1; 33
程式Using. LB (1/2) using System; namespace Using. LB { class Program { static void Main(string[] args) { bool x = 7 > 3; bool y = 2 < 0; Console. Write. Line("x = " + x); Console. Write. Line("y = " + y); bool x. ORy = x | y; Console. Write. Line("x | y : " + x. ORy); bool x. ANDy = x & y; Console. Write. Line("x & y : " + x. ANDy); 34
程式Using. LB (2/2) bool x. Oy = (x & y) | (x Console. Write. Line("(x & x. Oy); bool x. Ny = (x & y) & (x Console. Write. Line("(x & x. Ny); Console. Read. Line(); } } | y); y) | (x | y) : " + | y); y) & (x | y) : " + } 35
字串物件比較 string first = “one”; string second = “One”; string third = “one”; Console. Write. Line( first == == != != second ); third ); 37
一般邏輯運算 x y X & y X | y X ^ y !y false false true false true true false 38
程式 Using. Ter. Op using System; namespace Using. Ter. Op { /* * 用調分公式說明三元運算子的使用 * skj 3/4/2007 */ class Program { static void Main(string[] args) { int grade; int result; 41
程式 Using. Ter. Op "); Console. Write. Line("請輸入一個小於的整數原始成績: grade = int. Parse(Console. Read. Line()); result = grade < 60 ? 60 : grade; // 調分公式 Console. Write. Line("調分後成績: " + result); Console. Read. Line(); } } } 42
- Slides: 44