1221--VISUAL2012 basic operations, data types, type conversions

Source: Internet
Author: User

Basic operations

Output

Console.WriteLine (" This is a line of text "); Auto Enter.

Console.Write("Hello World"); without a carriage return.

Attention:

1. case sensitive. (Shortcut alt +→)

2. brackets, quotation marks, and semicolons are all symbols in the English state.

3 end do not forget to write a semicolon.

input

string s =console.readline ();

How to stitch a string.

Console.WriteLine (" your username is " + U + "and the password is " + p + ", please confirm.) ");

Other content

Console.foregroundcolor = consolecolor.green;// Set Font color

Console.backgroundcolor = consolecolor.gray;// Set background color

string s =console.readline ();

Console.clear ();// Clear Screen

Console.backgroundcolor = Consolecolor.yellow; Console.clear ();// Set screen background color

*******************************************************************************

First, the data type:

Strings--Put a string of characters. Need to be caused by "".

Integral type (int)--Integer type4bytesLong(Long plastic8word) Short(short integer type )2bytes)Tidy(micro-shaping1bytes)1bytes=8bit1b=8b 1byte=8bit

string s = "456";

String a = "789";

Console. Writeline (A+s);

int a = 456;

int b = 789;

Console. Writeline (A+B);

Decimal type, floating point type (flout,double)--

Float: Single-precision floating-point type. 4 bytes

double: dual-precision floating-point type. 8 bytes

Double d=3.14;

float d =3.14f;

Boolean type (bool). Logical type, either or. true,false;1 bytes

BOOL b =true;

BOOL d = False;

Character type (char). Single character

char c ='4';

Second, variable--the amount of value that can be changed during the operation.

Define first, then use

When you define a variable name, you cannot duplicate it.

(a) Definition:

Data type variable name "= value";

int a ;

int B = 20;

(b) Assignment:

Variable name = value;

(c) Value:

Directly using the variable name, you can directly take the values stored in the variable to be taken out.

(iv) General rules for naming variables.

1. variable names are generally composed of letters, numbers, and underscores.

2. the beginning can only be letters or underscores.

3. cannot repeat with system keywords.

Three, constant--the amount of value that cannot be changed during operation.

Literal constants.

Symbol constants. Definition: Add the const keyword to the left of the variable definition .

const int a = 456;

Note: The symbolic constants must be assigned when they are defined.

Application of Symbolic constants: in some of the repeated use of complex data, generally like to use represented instead of it, using constants for programming operations.

Four, type conversion.

The computer can only operate on the same type of data, the different types of data can not be directly computed, if it is different type, you need to convert (automatic, mandatory)

First, the value of A becomes a decimal type 10.0000, and then the division operation.

Automatic conversion: The computer automatically converts the type according to the operation data. The principle is that as long as the type is not lost, the data will be transformed.

Tidy->short->int->long->double

Forced conversions: The programmer forces a type to become another type. This forced conversion is implemented when the computer does not automatically convert, but it is possible to lose data.

Syntax: Add parentheses to the left side of the converted data, and the parentheses to write the target type to be converted.

1int a= (int)3.14

2 use convert.toxxx.

int e =10;double f =3.0;

int h =e/convert.toint32 (f)

string s = "87567";

int n = convert.toint32 (s);

Console.WriteLine (n + 1);

Instance

Example one: Calculate the area of a circle:const Double PI = 3.1415926;

Console.WriteLine (" Please enter radius ");

String r = Console.ReadLine ();

int R = Convert.ToInt32 (R);

Double S = PI * R * r;

Console.WriteLine (S);

Example two: Age Display:Console.WriteLine (" Please enter your age :");

String age = Console.ReadLine ();

int AG = Convert.ToInt32 (age);

Console.WriteLine (" your next year is " + (ag+1) + " years old ");

Points:

function needs main, in the same EXE file to save a window, and then create a class, you need to add The declaration of main to run, And you need to replace the previous window's main with a different name;

Conversions for data types are forced conversions (int a= (int)3.14 and use convert.toxxx. ) and automatic conversion (tidy->short->int->long->double);

Const Symbol Quantification: Significantly improves efficiency when you need to reuse or modify the same reference data.

1221--VISUAL2012 basic operations, data types, type conversions

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.