C # language basics 02

Source: Internet
Author: User
Tags arithmetic operators switch case

String:
String S = "AB ";
String S1 = "A \ Nb"; // n: the meaning of newline or next.

String S = "A \ B ";

String S = "C :\\ Temp \ fasdf \ dd \ AAA \ 1.jpg ";
Stirng S = "\\\\";

String [email protected] "\\\\"; // Adding @ in front of the string does not treat \ as an escape character.
String [email protected] "C: \ temp \ fasdf \ dd \ AAA \ 1.jpg ";

String [email protected] "AB" "; // error @ only valid for escape characters.


String STR = console. Readline ();
Console. writeline (STR );
Input: A \ Nb
Output: A \ Nb
The Escape Character is only applicable to strings directly written in the Code. It is not applicable to the strings read in the program.
C # basic programming 4
4convert type conversion

Console. writeline ("enter a number ");
String S1 = console. Readline (); // S1 cannot be declared as Int.
Int I1 = convert. toint32 (S1 );
Console. writeline ("enter a number ");
String S2 = console. Readline (); // S1 cannot be declared as Int.
Int I2 = convert. toint32 (S2); // convert. toint32 converts the string to an integer.

Console. writeline ("{0} + {1} = {2}", i1, I2, I1 + I2 );

Console. readkey ();

When learning, do not copy it by yourself. If you want to learn to change a little bit, you will learn a lot better. So much effort.

C # programming basics 5 exercise comments
Supplement:
Problem: int
I = convert. toint32 (console. Readline ());
String S1 = console. Readline ();
Int I = convert. toint32 (S1 );
@ Indicates a multi-line string.
Int I1 = convert. toint32 (console. Readline ());

Naming rules for variables: the first character must be a letter or underscore (_), followed by any number, letter, or underline. Not all C #
Such as Class/namespace/New/void. Judgment Method: keyword is highlighted in blue in.
This is also a naming rule for classes and functions.
You can use Chinese as the variable name (class name and function name ).
In C #, we recommend that the variable start with lowercase.

Which of the following is the correct variable name? _ A/A1/a_a/Clerk A/A1/1a/A3 _/A $ B/INT/int1/a B/A1/int
Variable Declaration: int I; int X, Y; in I = 3;

Int I1;
Int I2, I3;
Int I4 = 4; // declare the variable and assign the initial value at the same time.

Console. writeline (I1); // error. Before using a local variable, you must assign an initial value.
Console. writeline (I4 );//

I2 = 3;
Console. writeline (I2 );

C # programming basics 7 Arithmetic Operators

+,-, *,/, And % for remainder
+ It can be used as a string link. others cannot.
++ Auto-increment and -- auto-Subtraction
Operator priority: I1 + I2 * I3, (I1 + I2) * I3 do not
Abnormal. brackets are king.
The variable can be mixed with the word volume
Exercise: Ask the user to input two numbers and print the sum of the two numbers
Exercise: enter a radius to print the area of the circle.

Int I1 = 10;
Int I2 = I1 + 20; // The variable can be mixed with the word volume.
Console. writeline (I2 );

C # basic programming 8 variable assignment
Assign the value operator = to make the value of the Left variable equal to the calculation result on the right. This can explain the puzzling I = I + 1;
I2 = I1 ++; I2 = ++ I1;
Exercise: int A = 10; A ++; A = a + A; console. writeline ("{0}", a); what is the execution result?
Int I = 10; Int J = I; I = 5; j =?
Int x = 10; x + y = 80; console. writeline (y );
Exercise: exchange the values of two variables.

Int I = 10;
I = I + 1;
Console. writeline (I );
I = I + I;
Console. writeline (I );
Console. Readline ();

Int x = 10;
X + y = 80; // The variable on the left must be a constant or expression.
Console. writeline (y );

C # exchange of programming base 9 variables

Exchange the values of two variables:
Int I1 = 10, I2 = 20;
Console. writeline ("I1 = {0}, I2 = {1}", i1, I2 );
Int I3;
I3 = I1;
I1 = I2;
I2 = I3;
Console. writeline ("I1 = {0}, I2 = {1}", i1, I2 );

C # programming basics 10 Boolean operations

Equal judgment: =, do not confuse with =
Writeline ("{0}", I = 1); writeline ("{0}", I = 1); Difference
Unequal judgment :! =
Size comparison: <.>. <=.> =
Reverse :!
Combined Operation: & (and ). | (OR ).
& And: the value of the expression is true only when both sides are true. Otherwise, the value is false;
| Or: if either of the two sides is true, the expression value is true; otherwise, the expression value is false;

Int I = 30;
Bool B = (I = 1 );
Console. writeline ("{0}", I = 1 );
Console. writeline ("{0}", I = 1); // The value expression in C # also has a value. Its value indicates the value of the variable after the value is assigned.
Console. Readline ();

Output: false
C # basic programming Boolean operation 2 combination

 

Int I1 = 20;
Int I2 = 30;
Console. writeline ("{0}", I1> 10 & I2 <100); // The & value is true only when both sides are true. Otherwise, the value is false.

Console. writeline ("{0}", I1> 100 | I2 <100); // | or, if either of the two sides is true, true is used.
Console. Readline ();

// & And | the difference is that when either side is false and the other side is true.

C # programming basics 12 if statements
String S1 = console. Readline ();
Int age = convert. toint32 (S1 );
If (age> 20 ){
Console. writeline ("adult ");

}
Else {
Console. writeline ("minor ");
}
If (age> 20 ){
Console. writeline ("adult ");
}
Else if (age> 10 ){
Console. writeline ("children ");
}
Else {
Console. writeline ("baby ");
}
Console. readkey ();

 


If (age> 20 );
{
Console. writeline ("adult ");
}

If the input is 10, enter and output: Adult
Because the if statement has a semicolon, it indicates that the if statement has ended. And then run the print output statement in. The content in braces is irrelevant to if.

If (age> 20)
Console. writeline ("adult ");
Console. writeline ("Congratulations to adults ");
// Even if there is only one line of code in the IF statement, you must increase the brackets.

Learning programming is not about reading books, not about the teacher. Instead, write it by yourself.
Job 1: prompt the user to enter the password. If the password is "888888 ",
The prompt is correct. Otherwise, an error is prompted.
Job 2: prompt the user to enter the password. If the password is "888888", the prompt is correct. Otherwise, enter the password once. If the password is "888888", the prompt is correct. Otherwise, an error is prompted.
Job 3: prompt the user to enter the user name, and then enter the password at the prompt
If the user name is "admin" and the password is "888888", a correct prompt is displayed. Otherwise, an error is prompted. If the user name is not admin, a prompt indicating that the user name does not exist is displayed.
Job 4: the user is prompted to enter the age. If the age is greater than or equal to 18, the user is notified to view the information. If the age is less than 10, the user is notified to not be able to view the information. If the age is greater than or equal to 10, the system prompts you whether to continue viewing. If yes is entered, the system prompts you to view the information. Otherwise, the system prompts you not to view the information.

C # basic programming 13 14 exercise comments.

C # basic programming 15 switch case
The determined value is executed in the branch that meets the conditions.
Switch (I)
Case 1 ://
Break;
Case 2:
//
Break;

 

Switch (I)
Case 1:
//
Break;
Case:
//
Break;
Default:
Break;

Switch (I)
Case 1:
Case 2:
//
Break;
The value in case must be a constant, not a variable or expression.
It is similar to if... else, but it is a discrete value.
Both switch and premix can be rewritten with if, but if may not be used with switch
Rewrite.
Do not forget that the break in break. C # cannot be written. In addition to merging case.
Switch case example:
Console. writeline ("Please enter a number :");
String S1 = console. Readline ();
Int I = convert. toint (S1 );
Switch (I ){
Case 2:
Console. writeline ("You are really 2! "); Break;
Case 4:
Console. writeline ("go to hell! "); Break;
Case 8:
Console. writeline ("sending! "); Break;
Case 10:
Case 20: // equivalent to if (I = 10 | I = 20)
// When the input is 10 or 20, the output is the same. The only case does not require break.

Console. writeline ("You entered full money ");
Console. writeline ("really rich! ");
Default:
Console. writeline ("the {0} You entered is meaningless", I );
Break;
}
Console. readkey ();

String S1 = console. Readline ();
Int I = convert. toint32 (S1 );
Int K = 10;
Switch (k ){
Case I: // After case, it can only be a constant, not a variable.
Console. writeline ("what you entered is the same as what the program assumes ");
Break;
}

Related Article

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.