2-7-Data Operation Case study
Replication of variable values
Cases:
int x = 1;
int y = x; run →y=1
Variable values of the same type can be copied directly
code example:
Variable self-increment or self-subtraction
Cases:
int x = 1;
x = x+5;→ self-increment x=6
x = x-4;→ self-reduction x=2
Operation of multiple data
Cases:
int x=1,y=2;
x=x+y-1;→ operations from left to right →1+2-1→x=2
Note The return type of the operation
Error Example:
int x = +. Data of type 3+2;→double cannot be assigned to a variable of type int
int y = 1+ "1" Data of type +2;→string cannot be assigned to a variable of type int
Correct example:
Double x = +. 3+2;→x=4. 3
String y = 1+ "1" +2;→y=112
string x = "+1;→x=21"
int y = 3/2+1;→y=2
Double z = 1. 1*3/2;→z=1. 65
Exchange of variable values
Error Example:
int x=1,y=2;
X=y;y=x;
Correct example:
int x=1,y=2;
int z=x;
X=y;y=z;
2-8-Input statement
Write code for the input statement: Console.ReadLine ();
return type: String type → create string data for user-entered content
Example: static void Main (string[] args
{
Console.WriteLine ("Hello,world");
Console.ReadLine (); → program pauses execution, waiting for user input
}
string s = Console.ReadLine (); → Save (Console.ReadLine ()) to the string s
1, the program suspended, waiting for the user's input
2, the user input completed and press ENTER, get the user input string
3. Then save the string to the variable s
code example:
Console.ReadLine (s); → output s variable value
Summarize:
1, let the program suspend execution, waiting for user input
2. After the user presses the ENTER key, gets the content entered by the user, type string
3. After the user presses the ENTER key, the program continues execution, knowing that the main function ends
2-9-type conversion
Char?int
Char?string
Char?double
Int?string
Int?double
String?double
Sub-topic 1
Any type of →string
Conversion code: The data to be converted. ToString ();
data to be converted → data or variable to convert
Example: int a = 10;
String B = a.tostring (); →a is the data or variable that needs to be converted
Note: Data of type int cannot be assigned a variable of type string
return type →string
Double?int
Value range of the value range of double >int
Implicit conversions
numeric types with a small range of values → large numeric types are implicit conversions → no additional code required, the computer automatically completes the conversion
Example: int a = 123;
Double d = a;←int
Double d = 123;
Show Transformations (casts)
Double→int
Numeric types with large range of values → small numeric types are display conversions → additional code needs to be written and the computer does not automatically convert
Error Example: Double A = 3.14;
int d = A;
The correct example; Double A = 3.14;
int d = (int) A; →int d =3 (integer)
string→ number Types
String→int
Int.parse (string to be converted);
return type: int
String→double
Double.Parse (string to convert)
return type: Double
Sub-topic 5
2-10-Circular Area circumference calculator
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Circular Area Circumference calculator
{
Class Program
{
static void Main (string[] args)
{
Console.Write ("Please enter the radius of the circle (in meters):");
string input = Console.ReadLine ();
Double R = Double. Parse (input);
Double pai = 3.14;
Double ZC = 2 * pai * R;
Double MJ = Pai * R * r;
String JG = "Circumference of the Circle is:" + ZC + "M";
Console.WriteLine (JG);
JG = "The area of the circle is:" + MJ + "square meter";
Console.WriteLine (JG);
Console.Write ("Press ENTER to end the program");
Console.ReadLine ();
}
}
}
2-11-Transfer character
Double quotation marks in a string: string str = "text content";
Left quotation mark: string start
Right quotation mark: End of string
Error Example: Console.Write ("Tom said:" hello! "");
Change: Console.Write ("Tom said:\" hello!\ ");
Common escape characters
\ "→ Double quotes
\ ' → single quote
\n→ line break
Sub-topic 1
\t→ tab
Sub-topic 1
\\→ slash \
Sub-topic 3
2-12-number Types
Integer type
Real type
float and double
Floating point number
float→ single precision → up to 7 significant digits
double→ double precision → up to 15 significant digits
Numeric suffix
1.23→double
1.23f→float
1.23m→decimal
2-13-code Comment
Code comments: Use a few descriptive statements to make your code easier to read and understand
The content of the comment: there is no real function, not to participate in the compilation, only in the source code
Writing of code comments
Single-line Comment://comment Content
Multi-line Comments:
/*→ start of multiline comments
Comment Content
Comment Content
*/→ End of multiline comment
Cases
When to use annotations
1, the code is not easy to understand
2, the code is very complex
3. Large amount of code
4, in team collaboration development, there will be other people to read your code
Error and debugging in 2-14-software
Software error (resulting in source code not being compiled)
Error found: Viewing development tools vs errors
Resolve error: Make appropriate changes based on error prompts
Error features: Easy to find, easy to modify
Run errors (Error during operation)
Error found: Running in debug mode and running to the error statement
Resolve error: Make appropriate changes based on error prompts
Error features: Difficult to find, difficult to modify
Logic error (run result and expected)
Errors found: Check the results of the operation carefully to see if it is consistent with expectations
Resolve error: Use the VS breakpoint debug to check the execution results of each step
Error features: Extremely difficult to find, extremely difficult to modify
2-5-Data operations
Numeric operations
Arithmetic of numbers?
1, int integer
2, double decimal
Arithmetic rules
Supported operations: Plus (+), minus (-), multiply (*), except (/), remainder (%)
return type: Same as Operation type
Example: data type of the result of the operation
1+1← operations
2← operation Results
return type: int
1. type int and int type operation →int type
2, double type and double type operation →double type
3, double type and int type operation →double type
code example:
2-6-String arithmetic
A string? Any type of operation
Cases:
1, String?int
2, String?double
3, String?string
4, String?char
String operations
Support Operations: stitching (+)
Example: String 1+ string 2→ Result: String 1 string 2
code example: 1
code example: 2
return type: String (string concatenation with any type, return type is String)
2-4-Tips for using variables
Consolidated declarations
Declarations of multiple variables of the same type can be done in a single declaration
Cases:
Cases:
Initialize when declaring
Initializes a variable at the same time it declares a variable.
(initialization is the process of assigning a value to a variable for the first time)
Example 1:
Example 2:
Comprehensive Example:
Three elements of a variable
1. Data type
2. Variable name
3. Variable Value
Separate write: data type variable name;
Variable name = variable Value
Merge write: Data type variable name = variable value;
Missing three-factor examples:
Separate write is equivalent to merge write
2-3-Naming of variables
Name of the variable
Take a name for a variable
Syntax for variable definitions
Data type (char string int double) _ Variable name (Developer's own name);
Specification for variable naming
Hard Requirements
1. Variable names consist of numbers, letters and underscores
2. Variable name must not start with a number (example: int number2)
3. The name cannot be the same as the keyword (keyword: a word with a special purpose char string int double
/example: int int; int double; int char; int string;)
4. Variable names must not be the same in the same function
Variable names must not be the same in the same function:
Example: public static void Main (string[] args)
{
int A;
int A;?
int A;?
}
C # language Case sensitive A≠a
Soft requirements
Variable name to be able to look at the text to understand
Example: Define a variable that represents height (there may be decimals)
Height can be named as height there are decimals using double type
such as: double height;
The first letter of the variable name is lowercase
Capitalize other words except the first word
Example: Define a variable that represents a circular area (integer only)
Circular area: Circle areas integer with int type
such as: int Circlearea
Example: String username;
int Circlearea;
Double Customerowrmoney;
Hump Naming method
Example: String class;
class represents the category category synonym: 1, type 2, category
2-2-Using variables in your code
Recognize variables
A piece of memory that stores data, and the data content of that memory area can change
Declaration of a variable
Specifies a piece of memory space for storing data
Syntax: Data type (char string int double) _ variable name;
Used to hold data of the specified type
Example 1:int number;→ for storing integers
Example 2:string str;→ for storing strings
Assigning values to variables
Storing data in a variable's memory space
1, declaration: Data type variable name;
2, Assignment: variable name = data;
Example:
Composite Example 1
Composite Example 2
2-1-Data type
Char
Character type
1. Single text (Chinese characters, letters, numbers, punctuation marks)
2, the way of writing: in pairs of English single quotation marks
Correct example: ' Han ' a ' 2 '? ' ......
Error example: ' Hello ' ' ab ' 12 ',. ' ......
String
String type
1, unlimited number of words (Chinese characters, letters, numbers, punctuation marks)
2, the way of writing: in pairs of English single quotation marks
Correct example: "Han" "" "Hello,world"
Error example: ' Han ' ' hello,world '
int
Integer type
1, for short, to represent an integer
2, writing method: Direct writing
Correct example: 0 100-1-100
Error Example: 0. 0 1. 1-1. 2-1. 123
Double
Decimal type
1, used to denote a number containing a decimal point
2, writing method: Direct writing
Correct example: 0. 0 1. 1-1. 2-1. 123
Error Example: 0 100-1-100
Chapter II
2-1-Data type
Char
Character type
1. Single text (Chinese characters, letters, numbers, punctuation marks)
2, the way of writing: in pairs of English single quotation marks
Correct example: ' Han ' a ' 2 '? ' ......
Error example: ' Hello ' ' ab ' 12 ',. ' ......
String
String type
1, unlimited number of words (Chinese characters, letters, numbers, punctuation marks)
2, the way of writing: in pairs of English single quotation marks
Correct example: "Han" "" "Hello,world"
Error example: ' Han ' ' hello,world '
int
Integer type
1, for short, to represent an integer
2, writing method: Direct writing
Correct example: 0 100-1-100
Error Example: 0. 0 1. 1-1. 2-1. 123
Double
Decimal type
1, used to denote a number containing a decimal point
2, writing method: Direct writing
Correct example: 0. 0 1. 1-1. 2-1. 123
Error Example: 0 100-1-100
2-2-Using variables in your code
Recognize variables
A piece of memory that stores data, and the data content of that memory area can change
Declaration of a variable
Specifies a piece of memory space for storing data
Syntax: Data type (char string int double) _ variable name;
Used to hold data of the specified type
Example 1:int number;→ for storing integers
Example 2:string str;→ for storing strings
Assigning values to variables
Storing data in a variable's memory space
1, declaration: Data type variable name;
2, Assignment: variable name = data;
Example:
Composite Example 1
Composite Example 2
2-3-Naming of variables
Name of the variable
Take a name for a variable
Syntax for variable definitions
Data type (char string int double) _ Variable name (Developer's own name);
Specification for variable naming
Hard Requirements
1. Variable names consist of numbers, letters and underscores
2. Variable name must not start with a number (example: int number2)
3. The name cannot be the same as the keyword (keyword: a word with a special purpose char string int double
/example: int int; int double; int char; int string;)
4. Variable names must not be the same in the same function
Variable names must not be the same in the same function:
Example: public static void Main (string[] args)
{
int A;
int A;?
int A;?
}
C # language Case sensitive A≠a
Soft requirements
Variable name to be able to look at the text to understand
Example: Define a variable that represents height (there may be decimals)
Height can be named as height there are decimals using double type
such as: double height;
The first letter of the variable name is lowercase
Capitalize other words except the first word
Example: Define a variable that represents a circular area (integer only)
Circular area: Circle areas integer with int type
such as: int Circlearea
Example: String username;
int Circlearea;
Double Customerowrmoney;
Hump Naming method
Example: String class;
class represents the category category synonym: 1, type 2, category
2-4-Tips for using variables
Consolidated declarations
Declarations of multiple variables of the same type can be done in a single declaration
Cases:
Cases:
Initialize when declaring
Initializes a variable at the same time it declares a variable.
(initialization is the process of assigning a value to a variable for the first time)
Example 1:
Example 2:
Comprehensive Example:
Three elements of a variable
1. Data type
2. Variable name
3. Variable Value
Separate write: data type variable name;
Variable name = variable Value
Merge write: Data type variable name = variable value;
Missing three-factor examples:
Separate write is equivalent to merge write
2-5-Data operations
Numeric operations
Arithmetic of numbers?
1, int integer
2, double decimal
Arithmetic rules
Supported operations: Plus (+), minus (-), multiply (*), except (/), remainder (%)
return type: Same as Operation type
Example: data type of the result of the operation
1+1← operations
2← operation Results
return type: int
1. type int and int type operation →int type
2, double type and double type operation →double type
3, double type and int type operation →double type
code example:
2-6-String arithmetic
A string? Any type of operation
Cases:
1, String?int
2, String?double
3, String?string
4, String?char
String operations
Support Operations: stitching (+)
Example: String 1+ string 2→ Result: String 1 string 2
code example: 1
code example: 2
return type: String (string concatenation with any type, return type is String)
2-7-Data Operation Case study
Replication of variable values
Cases:
int x = 1;
int y = x; run →y=1
Variable values of the same type can be copied directly
code example:
Variable self-increment or self-subtraction
Cases:
int x = 1;
x = x+5;→ self-increment x=6
x = x-4;→ self-reduction x=2
Operation of multiple data
Cases:
int x=1,y=2;
x=x+y-1;→ operations from left to right →1+2-1→x=2
Note The return type of the operation
Error Example:
int x = +. Data of type 3+2;→double cannot be assigned to a variable of type int
int y = 1+ "1" Data of type +2;→string cannot be assigned to a variable of type int
Correct example:
Double x = +. 3+2;→x=4. 3
String y = 1+ "1" +2;→y=112
string x = "+1;→x=21"
int y = 3/2+1;→y=2
Double z = 1. 1*3/2;→z=1. 65
Exchange of variable values
Error Example:
int x=1,y=2;
X=y;y=x;
Correct example:
int x=1,y=2;
int z=x;
X=y;y=z;
2-8-Input statement
Write code for the input statement: Console.ReadLine ();
return type: String type → create string data for user-entered content
Example: static void Main (string[] args
{
Console.WriteLine ("Hello,world");
Console.ReadLine (); → program pauses execution, waiting for user input
}
string s = Console.ReadLine (); → Save (Console.ReadLine ()) to the string s
1, the program suspended, waiting for the user's input
2, the user input completed and press ENTER, get the user input string
3. Then save the string to the variable s
code example:
Console.ReadLine (s); → output s variable value
Summarize:
1, let the program suspend execution, waiting for user input
2. After the user presses the ENTER key, gets the content entered by the user, type string
3. After the user presses the ENTER key, the program continues execution, knowing that the main function ends
2-9-type conversion
Char?int
Char?string
Char?double
Int?string
Int?double
String?double
Sub-topic 1
Any type of →string
Conversion code: The data to be converted. ToString ();
data to be converted → data or variable to convert
Example: int a = 10;
String B = a.tostring (); →a is the data or variable that needs to be converted
Note: Data of type int cannot be assigned a variable of type string
return type →string
Double?int
Value range of the value range of double >int
Implicit conversions
numeric types with a small range of values → large numeric types are implicit conversions → no additional code required, the computer automatically completes the conversion
Example: int a = 123;
Double d = a;←int
Double d = 123;
Show Transformations (casts)
Double→int
Numeric types with large range of values → small numeric types are display conversions → additional code needs to be written and the computer does not automatically convert
Error Example: Double A = 3.14;
int d = A;
The correct example; Double A = 3.14;
int d = (int) A; →int d =3 (integer)
string→ number Types
String→int
Int.parse (string to be converted);
return type: int
String→double
Double.Parse (string to convert)
return type: Double
Sub-topic 5
2-10-Circular Area circumference calculator
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Circular Area Circumference calculator
{
Class Program
{
static void Main (string[] args)
{
Console.Write ("Please enter the radius of the circle (in meters):");
string input = Console.ReadLine ();
Double R = Double. Parse (input);
Double pai = 3.14;
Double ZC = 2 * pai * R;
Double MJ = Pai * R * r;
String JG = "Circumference of the Circle is:" + ZC + "M";
Console.WriteLine (JG);
JG = "The area of the circle is:" + MJ + "square meter";
Console.WriteLine (JG);
Console.Write ("Press ENTER to end the program");
Console.ReadLine ();
}
}
}
2-11-Transfer character
Double quotation marks in a string: string str = "text content";
Left quotation mark: string start
Right quotation mark: End of string
Error Example: Console.Write ("Tom said:" hello! "");
Change: Console.Write ("Tom said:\" hello!\ ");
Common escape characters
\ "→ Double quotes
\ ' → single quote
\n→ line break
Sub-topic 1
\t→ tab
Sub-topic 1
\\→ slash \
Sub-topic 3
2-12-number Types
Integer type
Real type
float and double
Floating point number
float→ single precision → up to 7 significant digits
double→ double precision → up to 15 significant digits
Numeric suffix
1.23→double
1.23f→float
1.23m→decimal
2-13-code Comment
Code comments: Use a few descriptive statements to make your code easier to read and understand
The content of the comment: there is no real function, not to participate in the compilation, only in the source code
Writing of code comments
Single-line Comment://comment Content
Multi-line Comments:
/*→ start of multiline comments
Comment Content
Comment Content
*/→ End of multiline comment
Cases
When to use annotations
1, the code is not easy to understand
2, the code is very complex
3. Large amount of code
4, in team collaboration development, there will be other people to read your code
Error and debugging in 2-14-software
Software error (resulting in source code not being compiled)
Error found: Viewing development tools vs errors
Resolve error: Make appropriate changes based on error prompts
Error features: Easy to find, easy to modify
Run errors (Error during operation)
Error found: Running in debug mode and running to the error statement
Resolve error: Make appropriate changes based on error prompts
Error features: Difficult to find, difficult to modify
Logic error (run result and expected)
Errors found: Check the results of the operation carefully to see if it is consistent with expectations
Resolve error: Use the VS breakpoint debug to check the execution results of each step
Error features: Extremely difficult to find, extremely difficult to modify
Web Front End Chapter II