C # Learning Lesson Two

Source: Internet
Author: User

#提纲:
Main function:
static void Main (string [] args)
{

}
The program code needs to be written in the curly braces of the main function.

First, output:
Console.WriteLine ("This is my first program.") ");
Console.WriteLine ("This is the second line of code. ");
Console.Write ("This is the text to be output. Note: No Line break
Attention:
1. Case sensitive;
2. All symbols are in English;
3. Don't miss out;

Second, enter:
string s = Console.ReadLine ();
This means reading a whole line of content from the console program into the container of S.
Console.WriteLine ("What you just entered is:" +s);
string-Types of strings
S-Variable name
=-value Assignment
Console.ReadLine (); -Reads an entire line from the console program


Third, comments and [uncomment]:
1. Select the appropriate line and click the comment in the toolbar to select the line [uncomment the selected row];
2. Select the appropriate line and use the shortcut key: Ctrl+k Ctrl + C [Ctrl+k Ctrl+u]
3. Manual Comment: Add//(note an entire line) in front of the line
4. Note One of the code:/* This is the code */
5. The function of the annotation://These two lines are the function of the output

Skills:
1. Write the code to use the IntelliSense function as much as possible;
2. First write the whole, then perfect the details;
3. Automatically complete the word function. alt+→

Case: Enter name, age, unit, and integrate into a fluent sentence display.
Console.Write ("Please enter Name:");
String a = Console.ReadLine ();
Console.Write ("Please enter Age:");
String B = Console.ReadLine ();
Console.Write ("Please enter the unit:");
String c = Console.ReadLine ();

Console.WriteLine ("My Name is +a+", this year "+b+" years old, in the "+c+" training. ");

My name is * * *, this year * * years old, in the * * training. "Put A,b,c into the * * *."
"My name is" +a+ ", this year" +b+ "years old, in the" +c+ "training. "

Data conversion
One, automatic conversion:

Second, the forced conversion:
Method 1: Add parentheses to the left side of the data being converted: (the data type to be converted)
Law 2:convert.toint32 (data to be converted);
Example: int d = (int) 3.14;
int d = Convert.ToInt32 (3.14);
float f = (float) 3.14;
float f = convert.tosingle (3.14);

string s = "3.14";
Double D = convert.todouble (s);
D = d+1;
Console.WriteLine (d);

One, arithmetic operators:
+ - * / % ++ --
Attention:
1. When doing a division, if two operations are integers, the result after the operation is an integer.
2. When doing arithmetic, if the two operands are not the same type, the type conversion will be done automatically at the time of operation.

Use of% modulus of redundancy
1. Determine if a is a multiple of B (can be divisible) ====>a%b whether ==0
2. Determine if the digits of a are b====>a%10 ==b
3. Turn a number into a number within a range (a theorem that uses the remainder to be larger than the divisor, such as the largest single number in hexadecimal is F (15))

++ --
int a = 5;
int b= a++; ====> can be written as int b =a; A = a+1;
int c = ++a;====> can be written as a = a+1; int c =a;
The result of the operation is
b = 5;
c = 7;
Note: + +-these two operations can only be applied to variables (5++ is not right!!) Constants are also not available!! )


Two, relational operators:
= = = > < >= <=
The result of the relational operator operation is all bool type (the result is either ture or false)

int a = 5;
int B = 6;
Console.WriteLine (A = = B); ====>false
Console.WriteLine (A! = b); ====>true
Console.WriteLine (a > B); ====>false
Console.WriteLine (a < b); ====>true
Console.WriteLine (a >= B); ====>false
Console.WriteLine (a <= B); ====>true

Note: = = cannot be written as = (A = is the meaning of an assignment)


Three, logical operators:
&& | | !
&&====> Two conditions are true, and the result is false otherwise
|| ====> two conditions as long as there is a set, the result is true, two are not set to False
! ====> The original result to the opposite (if it is true, the inverse is false)

Iv. Other operations
= ====> Assignment operation, assigns the following value to the left variable. (Note: only variables)
+ = = *=/=%= ====> composite Operator (example: A + = B; (meaning a = a +b;))

Arithmetic operators
//++ --
Placed before or after a variable name
Placed at the back of the time
int a = 1;
int B = A++;//int b=a;a=a+1
int c = ++a;//a=a+1;int C=a
Console.WriteLine (a);
Console.WriteLine (b);
Console.WriteLine (c);


//*............/........%
//%... Mode.... In addition to the business to take surplus
1. Determine if a is a multiple of B (can be divisible) ===>
int a = 10;
int b = 3;
Console.WriteLine (a%b);

// +....-
int a = 3;
int b = 4;
Console.WriteLine (A-B);


Relational operators
>......<....>=...<=
//==.....! =
int a = 4;
int B = 6;
BOOL C = a < b;
Console.WriteLine (c);

logical operators
&& two are satisfied
//|| There is at least one satisfying
//! If the start is true, change to FA
int a = 4;
int B = 7;
int c = 8;
BOOL D = A < b && a < C;
BOOL E= a < B | | a < C;

Console.WriteLine (d);
Console.WriteLine (e);

Conditional operator ... ;
int a = 3;
int B = 6;
string s = a < b? "Yes, a<b": "Wrong, a>b";
int c = a > B? 1:2;
Console.WriteLine (s);
Console.WriteLine (c);


Assignment operators
//=...+=...-=.. /=...*=...%=

int a = 3;
A + = 2;//a=a+2
A-= 5;//a=a-5
A *= 2;//a=a*2
Console.WriteLine (a);

Conditional operators, practice
Q, what time is it now?
Enter only the number of hours

Console.Write ("What's The Time Now:");
int shi = Int. Parse (Console.ReadLine ());

if (Shi >= 0 && shi <= 24)
//{
string mm = Shi > 12? "Now is PM" + (shi-12): "AM";
Console.WriteLine (mm);
//}
Else
//{
Console.WriteLine ("Your input is wrong");
//}

Console.ReadLine ();

C # Learning Lesson Two

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.