1. Variables
Naming rules:
A can only be a letter (A-Z), Number (0-9), underscore (_)
b cannot start with a number
c cannot be a keyword in C #
Naming conventions:
A short, semantic word or combination
B Camel nomenclature (start with the second word, capitalize the first letter) MyName
Steps to use:
A Declaration and Assignment (example: int age=18)
b Use (input/output, judgment, arithmetic, etc...) )
* First value and then use
2. Data type
Shaping and non-shaping:
Reshape: sbyte (signed -128~127) byte (0~255) byte 8 bit
Short ( -32768~32767) ushort (0~65535) 2 bytes 16 bit
Int ( -2^63~2^63-1) UINT (0~^32-1) long ( -2^63~2^63-1)
Non-cosmetic:
FLOAT: single-precision floating-point type F
Double: dual-precision floating-point D
Decimal:m
Non-numeric:
Char: Storing a single character
String: Strings of arbitrary length caused by double quotation marks
BOOL: An expression that stores true or false, or that results in a bool value
A + Connector connection string
B placeholder {number} number: 0~ number of variable list-1
Console.WriteLine ("Formatted string", variable list);
Supplemental (Understanding Binary):
Decimal number calculation features: every ten into one
Binary of the computer:
236:3*10^2+3*10^1+6*10^0
01001001:0*2^7+1*2^6+1 ..... (binary turns into decimal)
Octal: Shortening binary
Hex: Color
3. Operators
+ 、-、 *,/, &&, | |, <, >, <=, >=, + + (self-increment) 、--(self-reduction),% (remainder),! =
Cases:
int n=1,m=1;
To do the suffix, reference the original value of the variable, and then increment it; (First operation + 1) int x=n;n++
int x=n++;
Do the prefix, perform the self-increment operation and then use the value of the variable, (first +1 and then the operation) M++;int Y=m
int y=++m;
Console.WriteLine ("X={0}\ny={1}", X, y);
Console.readkey ();
logical operators
&& (logic and): conditional expression 1&& conditional expression 2 satisfies both (two conditions are true and the result is true)
|| (logical OR): conditional expression 1| | Conditional expression 2 or (as long as one condition is true, the result is true)
! (logical non):! Conditional expression, if the condition expression is true, the result is false, if the conditional expression is false, the result is true
Example: Console.WriteLine ("Input Score");
int writescore = Int. Parse (Console.ReadLine ());
Console.WriteLine ("Input Score");
int labscor = Int. Parse (Console.ReadLine ());
BOOL Isparse = Writescore >= && labscor >= 60;
Console.WriteLine (Isparse);
Console.readkey ();
numeric operators
Shortcut operators: + =,-=, *=,/=
Cases:
Sum+=1 equals sum=sum+1;
Ternary operators
Cases:
Console.WriteLine ("Please enter a number");
int Num1=int. Parse (Console.ReadLine ());
Console.WriteLine ("Please enter a number");
int num2 = Int. Parse (Console.ReadLine ());
int max = NUM1 > num2? num1:num2;
Console.WriteLine ("The largest of the two numbers is {0}", max);
Console.WriteLine ("Please enter a number");
int num3 = Int. Parse (Console.ReadLine ());
max = max > num3? MAX:NUM3;
Console.WriteLine (the largest of the three numbers is: {0} ", max);
Console.readkey ();
Exercises on logical operators:
Receives the year of keyboard input, determines whether the year is a leap years?
Conditions for leap years: The year can be 4 full but not divisible by 100, or divisible by 400.
Code:
The first of these methods
Console.WriteLine ("Please enter a year?");
int year = Int. Parse (Console.ReadLine ());
BOOL Isyear = year% 4 = = 0 && Year% 100! = 0 | | Year% 400 = = 0;
Console.WriteLine (Isyear);
Console.readkey ();
String isyear = year% 4 = = 0 && Year% 100! = 0 | | Year% 400 = = 0? " Leap year ":" Common Year ";
Console.WriteLine (Isyear);
Console.readkey ();
The second method of
if (year% 4 = = 0 && year%!! = 0 | | year% 400 = = 0)
{
Console.WriteLine ("{0} years is a leap year";
}
Else
{
Console.WriteLine ("{0} is common year", year);
}
Console.readkey ();
Operator Classifications:
A unary operator
b Two-dollar operator
C Ternary operator
4. Type conversion
Between numeric types:
Implicit conversions (auto-conversion): type-compatible target types are larger than the source type (range) from small to large (conversions do not fail and do not lose precision)
Explicit conversions (casts): May fail, or may lose precision
Syntax for casting:
(target type) expression;
String types are converted to other types of-->xxx. Parse (string); XXX---types
The type of data you want to get. Parse ()
Cases:
String str= "1";
int I=int. Parse (str);
Double i=double. Parse ();
Float i=float. Parse ();
Other types are converted to string types:-->xxx. ToString (); XXX---(expression, value, variable name)
The variable name. ToString ();
Any type conversion
The usual Convert method:
int I=convert.toint32 (15.22);(similar to rounding)
float F=convert.tosingle ();
Double d=convert.todouble ();
BOOL B=convert.boolean ();
Summarize:
1. Variables
Data type variable name = value
You must first assign a value when using a variable
The assigned value and the variable's data type match the int i=2;double d=2.5;
Naming rules and specifications for variables
2. Data type
Numeric non-numeric
Value: Shaping non-reshaping
Shaping: styte byte short ushort int UINT long ULONG
Non-reshaping: float double decimal
Non-numeric: char string bool
3. Operators
One yuan, two yuan, ternary
Arithmetic operators
Comparison operators
Conditional operators
Assignment operators
Shortcut operators
Priority level: () > Unary operator (! + +-) > Arithmetic operator (*/%>+-) comparison operator (>, <, >=, <= precedence over = =,!) =) > Logical operators (&&, | | ) > Assignment and shortcut operators
4. Type conversion
Between numeric types
Implicit conversions: (Auto-convert) double d=20;
Explicit conversions: (CAST) int i= (int) d;double d1= (double) I;
Strings <----> Other types
Xxx. Parse (String) string: Must be a valid representation format for the destination type XXX: type
Xxx,tostring (); XXX: variables, expressions, values
Convert.toint ();
Homework:
1. Temperature Converter
Fahrenheit = (C-32) *5/9, require input date, Celsius, output "today is * * *, temperature is * * * Fahrenheit";
Double f=9.0/5*c+32
Code:
Console.title = "Temperature Converter";
Console.WriteLine ("Welcome to the temperature Converter! ");
Console.WriteLine ("Please enter today's temperature (degrees Celsius)");
Double s = convert.todouble (Console.ReadLine ());
Double F = 9.00/5 * s + 32;
Console.WriteLine ("Today is: {0}\n Today's temperature is {1} degrees Fahrenheit", DateTime.Now.Date, F);
Console.readkey ();
2. Decomposing integers
Receives a 4-bit integer from the keyboard input, outputting bits, 10 bits, hundred, and thousands, respectively.
such as: 1234 respectively output bits: 40 bit: 300 bit: 2000 bit: 1
Except 10 becomes the lowest bit to take the remainder
Code: Console.WriteLine ("Please enter a four-digit integer!");
int Number=convert.toint32 (Console.ReadLine ());
int nb1=number%10;//Bits
int nb2 = number/10% 10;//10 bit
int nb3 = number/100%10;//Hundred
int Nb4 = number/1000;//thousand
Console.WriteLine ("single digit is {0}, 10 digits is {1}, the number of hundred is {2}, Thousand is {3}", Nb1, Nb2,nb3,nb4);
Console.readkey ();
C # variable/data type conversion/operator use