2014-12-14 跳转语句,while循环,各种类的用法

跳转语句:break

例:for(int i=0;;i++)  //无判断条件,即永远满足条件,无限循环

{

console.writeline("hello world!");

if(i==9)

{

break;    //i=9时跳出循环

}

continue:

if (i==2)

{

continue;  //结束continue后面的后续语句,直接跳往状态改变,开始下一次循环

}

 

while循环:返回true或false

int i=0;  //初始条件

while(i<=9)

{

console.writeline("hello");

i++;   //状态改变(写在大括号里面)

}

do...while...

int i=0;

do{

i++;

console.writeline(i);

} while (i<0);   //输出结果为1,因为条件判断在后,执行过程在前,每次需先执行一遍i++,再判断i<0。

 

异常语句:防止程序中断,继续执行后面的内容

while(true)

{

int i;

try

{

string s="1.23";

int i=int.parse(s);

---(console.writeline(i);   //1.23无法转换为int,会报错)---

}

catch(exception e)   //exception为类,e是自己命名的一个,括号中可写任何

{

console.writeline(e.massage);   //输出错误原因

continue;

}

console.writeline(i);

break;

}

 

或者:

catch(exception e)  

{

console.writeline(e.massage);  

}

finally

{

console.writeline(i);

console.writeline("不管错误如何,最终都会执行finally");

}

 

类型

String 类:String类是一个class类型的类,里面包含许多处理字符串的方法和属性

String

string s="hello";

int i=s.Length;  //获取字符串的长度,返回int值

console.writeline(i)  //输出结果为5

string s="      hello     "

s=s.Trim();    //去除字符串前面和后面的空格

s.TrimStart()   //去除前面的空格

s.TrimEnd()    //去除后面的空格

s.ToUpper()    //转换为大写

s.ToLower()    //转换为小写

 

字符串有索引,从0开始

string ss="abcdef123";

int cc=ss.IndexOf("cd");

console.writeline(cc);   //结果为2,从0开始,0、1、2,cd 在2的位置

 

string ss="abcdef1cd23";

int cc=ss.LastIndexOf("cd");

console.writeline(cc);  //结果为7,last查的是最后一个cd的位置,从0开始 

 

bool b1=ss.StartsWith("ab")    //判断一个字符串是否以ab开头

bool b2=ss.EndsWith("23")    //判断是否以23结尾

console.writeline(ss.Contains("cd"))   //判断字符串中是否包含cd字符段

 

Substring:截取字符串

ss.Substring(2,3);  //从 2(第三个位置) 的位置开始截,截取3个

ss.Substring(2)   //从2的位置开始,截取到最后

 

math类

i=math.Abs\sqrt\ceiling\floor\round\pi (i)

 

datetime类

datetime d=new datetime(1990,01,01);

timespan t=new timespan(3,0,0,0);   //括号里面为添加 天,时,分,秒

d=d.add(t);

或者直接使用下面的语句直接添加时间:  d=d.adddays(3);   addhours…… 等等

datetime.now  //获取系统当前时间

int s=d.DayofYear;

console.writeline(s); //今天是今天的第多少天,返回int型

console.writeline(d.dayofweek);   //返回 sunday,周几

 

string ss=d.ToString("yyyy年MM月dd日hh时mm分ss秒");

double dd=1.234;

ss=dd.ToString("#.00");  //#为整数位部分, .00为小数点后补两位0,或者#.#

例:判断日期是否正确

try

{

datetime dt=datetime.parse(console.readline());     //若不能转化时,日期格式不正确

console.writeline(dt);

}

catch

{

console.writeline("不是正确的有效日期")

}

console.Clear(); //清空信息

例:手机号抽奖

string s="12333333333"

for(int i=0;i<=100,i++)

{

console.Writeline(s);

thread.sleep(20); //括号里的为毫秒,整句意为延时多少毫秒

console.clear();

console.writeline("14566667777")

thread.sleep(20);

console.clear();

}

posted @ 2014-12-14 14:36  雪山飞驴  阅读(188)  评论(0编辑  收藏  举报