2014-12-14 Jump statement, while loop, usage of various classes

Source: Internet
Author: User

Jump statement: Break

Example: for (int i=0;; i++) //no judgment condition, i.e. forever satisfying condition, infinite loop

{

Console.WriteLine ("Hello world!");

if (i==9)

{

Break Jump out of the loop when i=9

}

Continue

if (i==2)

{

Continue //End Subsequent statements after continue, jump directly to the state change, start the next loop

}

While loop: Returns True or False

int i=0; Initial

while (i<=9)

{

Console.WriteLine ("Hello");

i++; //State change (written in curly braces)

}

Do...while ...

int i=0;

do{

i++;

Console.WriteLine (i);

} while (i<0); //The output is 1, because the condition is judged after, the execution process before, each time needs to execute once i++, then Judge I<0.

Exception statement: Prevent program interruption, continue to execute later

while (true)

{

int i;

Try

{

String s= "1.23";

int I=int.parse (s);

---(Console.WriteLine (i); //1.23 cannot be converted to int, will error)---

}

catch (Exception e) //exception is a class, E is one of its own names, and the parentheses can write any

{

Console.WriteLine (E.massage); //Output error reason

Continue

}

Console.WriteLine (i);

Break

}

Or:

catch (Exception e)

{

Console.WriteLine (E.massage);

}

Finally

{

Console.WriteLine (i);

Console.WriteLine ("Eventually will execute finally" regardless of the error);

}

Type

String Class: The String class is a class that contains many methods and properties for handling strings

String

String s= "Hello";

int i=s.length; Gets the length of the string, returning an int value

Console.WriteLine (i)//output result is 5

String s= "Hello"

S=s.trim (); //Remove spaces before and after the string

S.trimstart () //Remove the front space

s.trimend () //Remove the trailing spaces

s.toupper () //Convert to uppercase

s.tolower () //Convert to lowercase

string is indexed, starting from 0

String ss= "abcdef123";

int Cc=ss. IndexOf ("CD");

Console.WriteLine (CC); //result is 2, starting from 0, 0, 1, 2,cd in 2 position

String ss= "Abcdef1cd23";

int Cc=ss.Last IndexOf ("CD");

Console.WriteLine (CC); //Results for 7,last the location of the last CD, starting from 0

bool B1=ss. StartsWith ("AB") //Determine if a string starts with AB

bool B2=ss. EndsWith ("All") //judge whether to end with 23

Console.WriteLine (ss. Contains ("CD")) //Determine if the string contains a CD character segment

Substring: Intercepting strings

Ss. Substring (2,3); //Starting at 2 (third position), intercept 3

Ss. Substring (2) //starting from 2 position, intercept to the last

Math class

I=math. ABS\SQRT\CEILING\FLOOR\ROUND\PI (i)

DateTime class

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

TimeSpan t=new TimeSpan (3,0,0,0); //parentheses inside for adding days, hours, minutes, seconds

D=d.add (t);

or directly add the time directly using the following statement: D=d.adddays (3); AddHours ... Wait a minute

DateTime.Now //Get system Current time

int s=d.dayofyear;

Console.WriteLine (s); //Today is today's number of days, return int type

Console.WriteLine (D.dayofweek); //Return to Sunday, week

String ss=d.tostring ("yyyy mm month DD day hh" mm min ss sec ");

Double dd=1.234;

Ss=dd.  ToString ("#.00"); //#为整数位部分,. 00 is two digits after the decimal point 0, or #.#

Example: Judging whether the date is correct

Try

{

DateTime dt=datetime.parse (Console.ReadLine ()); //If not converted, date format is incorrect

Console.WriteLine (DT);

}

Catch

{

Console.WriteLine ("Not the correct effective date")

}

Console. Clear (); Clear information

Example: Mobile number Lottery

String s= "12333333333"

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

{

Console. Writeline (s);

Thread.Sleep (20); //In parentheses for milliseconds, the whole sentence means how many milliseconds to delay

Console.clear ();

Console.WriteLine ("14566667777")

Thread.Sleep (20);

Console.clear ();

}

2014-12-14 Jump statement, while loop, usage of various classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.