C # Summary (II)

Source: Internet
Author: User

Exception Capture:

Try

{

The error code may be written here.

}

Catch

{

Post-error handling

}

How to run the above program:

If no error occurs in the code in try, the program runs normally.

The code in try will not execute the code in catch; otherwise

Directly jump to catch to execute the code!

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2113102E7-0.png "title =" ard.png "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/211310Na-1.png "title =" clip 文 talent d.png "/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/21131029B-2.png "title =" 123oard.png "/>

Class Program {static void Main (string [] args) {try {Console. WriteLine ("enter your name! "); String name = Console. readLine (); Console. writeLine ("Enter your language score:"); int chinese = Convert. toInt32 (Console. readLine (); Console. writeLine ("Enter your mathematical score:"); int maths = Convert. toInt32 (Console. readLine (); Console. writeLine ("Enter your english score:"); int english = Convert. toInt32 (Console. readLine (); int sum = chinese + maths + english; double average = 1.0 * (chinese + maths + english)/3; Console. writeLine ("{0} ! Your total score is {1} and the average score is {2} ", name, sum, average);} catch {Console. writeLine ("the data you entered is incorrect. Please enter it again:");} Console. readKey ();}}



Class Program {static void Main (string [] args) {Console. writeLine ("Enter the number of days you want to calculate:"); int number = Convert. toInt32 (Console. readLine (); int week = number/7; int day = number % 7; Console. writeLine ("{0} Days is {1} week 0 {2} Days", number, week, day );}}


Class Program {static void Main (string [] args) {Console. writeLine ("Enter the time in seconds you want to calculate):"); int second = Convert. toInt32 (Console. readLine (); int day = second/(24*3600); int hour = second/3600; int minute = second/60; int second_1 = second % 60; Console. writeLine ("{0} seconds is {1} Days {2} hours {3} minutes {4} seconds", second, day, hour, minute, second_1 );}}


Operator

1. Auto-increment and auto-increment:

++ Auto-increment has a plus minus -- auto-increment has a minus Int age = 18; int sum = age ++-10; int sum = ++ age-10; int sum = age ---10; int sum = -- age-10; add or subtract the original value after calculation, calculate the value of add or subtract. However, age ++ age -- both perform auto + 1 or auto-1. In C #, the priority of the unary operator is generally greater than that of the binary operator.
int a = 1;a++; // a = a + 1int b = a;a--; // a = a - 1int c = a;Console.WriteLine("a++ : {0}", b);Console.WriteLine("a-- : {0}", c);

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2113104c2-3.jpg "title =" 2.jpg"/>

2. Compound operators:

+ = Example: age = age + 3; <=> age + = 3; directly add 3-= example: age = age-3; <=> age-= 3; directly subtract 3 * = for example, age = age * 3; <=> age * = 3; multiply by 3/= For example: age = age/3; <=> age/= 3; directly divide by 3% on age = For example: age = age % 3; <==> age % = 3; get the remainder directly from 3 on age

In C #, the values in the variables that can be changed include =/++ /--

Int a = 10; a = a + 10; // assign the value of a to a (10) + 10 // after the operation is complete, a = 20Console. writeLine ("a = a + 10; a = {0}", a); a = 10; // restore a back to 10a + = 10; // equivalent to a = a + 10; assign the value of a to a (10) + 10 // after the operation is complete, a is equal to 20 console. writeLine ("a + = 10; a = {0}", );

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/21131051D-4.jpg "title =" 3.jpg"/>


3. Relational operators comparison operators)

> <//> Indicates greater than the number, and <indicates smaller than the number. =! /// = Two equal signs indicate equal to, and one equal sign indicates a value ,! = Indicates not equal. >=<=// >=Indicates greater than or equal to, <= indicates less than or equal. In C #, there are six Relational operators used to compare the relationships between two things. Relational expressions: The formulas connected by Relational operators are called relational expressions.


4. boolean (bool) type:

The bool value has only two values. 1. true: true. 2. false: false. If the relational expression is true, the value of this expression is true. Otherwise, it is false.


5. logical operators:

The logical operator has one or two Boolean operations and returns a boolean result. Logical operators allow you to combine multiple boolean expressions to form other boolean expressions.

Logical operators include |, &, AND ^, which correspond to OR, AND, XOR, respectively.

Logical Inverse Operator !) It is also called the NOT operator. It is used to reverse a value of the bool data type. It is a unary operator and only needs one operand.

1. OR operator |)

| The operator evaluates two boolean expressions. If either of them is true, true is returned.

2. AND operator &&)

Boolean AND operator & requires that true be returned only when both operands are evaluated as true. If any operand is false, false is returned.

3. XOR operator ^)

^ The symbol is an exclusive or operator. If two Boolean operands are used, onlyOnlyIf the value is true, the XOR operator returns true.

Unlike Boolean and or, the Boolean XOR operatorShort-circuit computation not supported. It always hasCheck the two operands.Because the final result cannot be determined unless the values of the two operands are exactly known.


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2113106205-5.png "title =" clipbo ard.png "/>

Class Program {static void Main (string [] args) {Console. writeLine ("Enter the year to be judged:"); int year = Convert. toInt32 (Console. readLine (); bool result = (year/400) = 0 | (year/4 = 0 & year/100! = 0); Console. writeLine ("if the year entered by the user is a runyear, TRUE is output. If it is not a runyear, FLASE is output. result: {0} ", result); Console. readKey ();}}



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.