using System; //引入命名空间
namespace LanOu0803 //命名空间
Console.WriteLine ("Hello World!"); //输出方法
读取一个字符,随意按下一个字符后终止输入的操作
int n = Console.Read();
Console.WriteLine ("刚刚输入的n的值为{0}",n);
读取一行字符,按回车结束输入操作
string str = Console.ReadLine ();
Console.WriteLine ("刚刚输入的str的值为"+str); //字符串只能用加号
Console.WriteLine("你好\n"); // \n换行符
Console.Write("刚刚");
类型转化
隐式转化 、
float a = 1; //单精度
double b = 1.2f; //双精度
a = (float)b; //高精度转低精度
强制转化
double speed = 10.4f;
float minSpeed = (float)speed;
int c = (int)1.4f;
string num = "123";
int d = int.Parse (num); //string强转int ,保证纯数字
string num = "123";
int m = Convert.Tolnt16(num);
float a =10.4f;
int b = (int)a;
string str1 = 123456.ToString (); //任何类型转string
string str2 = b.ToString (); //Tostring() 方法
string str3 = a.ToString ();
string str = "123a";
int c = 0; //int.TryParse 与 int.Parse 又较为类似,但它不会产生异常,转换成功返回 true,转换失败返回 false。
if (int.TryParse (str, out c)) { //尽量避免崩溃发生 c为参数是输出参数,把结果 str 放入 c
Console.WriteLine ("{0}", c);
} else {
Console.WriteLine ("failure"); //崩溃
}
获取内存占用 bytes(字节) bits(位,比特)
1字节 == 8比特位 1汉字 == 2字节 1英文 == 1字节
//0符号位 0为正 1为负
//0000000
//01111111 => 127
//sbyte => -127 —127
//byte =>0 — 255
//基本数据类型
float a = 1234E-2F; //12.34 2F表示小数点后两位
float a = 2.5246f,b = 3.45f;
// Console.WriteLine ("{0:0000.000},{1:C}",a,b);
Console.Write ("{0:f3},{1:F1}",a,b);
//a = 2.525 b = 3.5
Console.Write ("{0:P},{1:p3}",a,b);
//a = 252.46 b = 345.000
//求字节大小
Console.WriteLine("int ->{0}",sizeof(int)); //4字节
Console.WriteLine("double ->{0}",sizeof(double)); //8字节
运算符
+ - * / % += -= *= /= >< >= <= ++ -- ?: && \\ !
& \ ^ << >> ==
int a = 1,b = 2,c = 3;
int b = 2;
int c = 3;
c = a + b;
float d = 1.0f;
c = c - a;
c -= a; //c= c - a;
a += b; //a = a+ b;
float e = a * d;
a *= b;//a = a * b;
a /= b;//a = a / b;
Console.WriteLine ("{0}",e);
求余运算符 %
int a = 34,b = 45;
Console.WriteLine ("{0}",b % a); //11
Console.WriteLine ("{0}",b / 10); //4
强转(a % d)的值
float c = 1.2f, d = 2.4f;
int e = (int)(a % d);
比较
int a = 34,b = 45;
bool c = a > b;
c = a < b;
c = a >= b;
c = a <= b;
c = a != b; //不等于
c = a == b; //恒等于
Console.Write("{0}",c);