基本數據類型和表達式的學習

1.需要了解的知識:

1字節(bety)== 8比特位


2^10 =1024? ? ? ? ? ? 2^8-1=11111111

有符號位:sbyte :? -127~127

無符號位:byte :? 0~255? ? ? ? ? 11111111

char c = 'a';(字符類型)

sizeof ? (字節大小計算)

Console.WriteLine("{0}",sizeof (int));


2.基本數據類型:

float a = 1234E-2F;a =12.34

int a =1;

float b = 1.0f;

double c = 1.0;

char d = 'n';

bool e = true;

string f = "hello";

3.交換數值:

int a = 2, b = 3;

a = a + b;

b = a - b;

a = a - b;

Console.Write ("a={0},b={1}",a,b);(a =3,b=2)

4.運算符? ? +? -? /? *? ? ++? --? ? % :

int a = 3,b = 6;

int c = a + b;

float e = 5.5f;

float d = a + e;//高精度

d = e % a ;

Console.WriteLine ("{0}",d);

a++;

--b;


5.表達式都有一個結果,稱為返回值

const int f = 3+5 ;(f 的值不會再變,在變就會報錯)

Console.WriteLine ("小明:\"你好,我是牛明\"");

float a = 2.5246f, b = 3.45f;

Console.Write ( "{0:00.00},{1:c}",a,b);

Console.Write ( "{0:f3},{1:F1}",a,b);

Console.Write ( "{0:P},{1:p3}",a,b);

int n = Console.Read () ;(讀取一個字符,它是屬于int類型)

int n = Console.Read();

string str = Console.ReadLine ();

6. 6.1隱式轉化

double speed = 10.4f;

?float minSpeeed = (float)speed;

?6.2強制轉換

float a =10.4f;

int b= (int) a;

Console.WriteLine ("{0}",b);

? ?string str = "123";

? ?b = int.Parse (str);

Console.WriteLine ("{0}",b);

? ?int c =0;

? ?string str = "123a";

? ?if (int.TryParse (str, out c)) {

? ? Console.WriteLine ("{0}", c);

? ?} else {

? ? Console.WriteLine ("failure");

? ?}

6.3convert強制轉換


c= Convert.ToInt32 (str);

Console.WriteLine ("{0}", c);

6.4另一種轉化方法:int和float 轉化為Sting型的方法:

string str1 = 12345.ToString ();

string str2 = b.ToString ();

string str3 = a.ToString ();

Console.WriteLine (str3);

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容