C#类型装换&分支

DateTime dt = new DateTime(2017,6,7,13,56,0); 获取输入的时间
Console.WriteLine(dt); 去掉line在一行显示
DateTime dt_now = Date.Now; 获取现在的时间
Console.WriteLine(dt_now);
string dt_str = dt.ToString(); 将时间转换成字符串
Console.WriteLine(dt_str);
doublei_dou = 123; 数字之间转换
int i_int = (int)i_dou;    该转换方式主要用于数字类型之间的转换,从int类型向long,float,double,decimal 类型转换可以使用隐式转换,但从long型到int 就需要使用显示转换,即使用该类型的转换方式否则产生编译错误。
string i_str ="321" ; 
int i_int2 = int.parse(i_str); parse强制转换该方式是将数字内容的字符串转换为int类型,如果字符串的内容为Null ,则抛出ArgumentNullException异常;如果字符串内容不是数字,则抛出FormatException异常。
int i_inr3 = Convert.ToInt32(i_str); 该方式不仅可以将字符串类型转换为int,还可以将其他的类型转换为int。变量若为object或string类型,当其值为Nul时,会传回0,不会造成程序错误,但是若此string类型的值为string.Empty,转换成int时,程序仍会出错。
double i_dou2 = Convert.ToDouble(i_str);     推荐

const int x = 1;  常量只能赋值一次

Console.WriteLine("\"");
Console.ReadLine(); 防止闪退

常用的转义字符及其含义:
\’ 单引号
\” 双引号
\\ 反斜杠
\0 空
\a 警告(产生峰鸣)
\b 退格
\f 换页
\n 换行
\r 回车
\t 水平制表符
\v 垂直制表符

int x = int.parse(Console.ReadLine());          Console.ReadLine()是字符串;

if(x==1){

  Console.WriteLine("1");

}else if(x==2){

   Console.WriteLine("2");

}else{

    Console.WriteLine("3");

}

int x =true ?1:2  三元运算符    如果是true是第一个不是的话是第二个

switch(y){

   case 1:

       break;

   case 2:

      break;

   default:

      break;

}

for (int i = 0;i<10;i++){

   Console.WriteLine(i);

}

Console.ReadLine(); 防止闪退

posted @ 2017-06-07 16:45  纡ゾ少︶ㄣ  阅读(146)  评论(0编辑  收藏  举报