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);