Data conversion Type:
Other types of conversion value types: Convert.ToInt32 (the variable to be converted);
Int. Parse (the variable to convert);//Small range
(int) variable//not useful
Example: string converted to int
Other types (common: int,decimal) Convert String type: variable. ToString ();
This variable can also be converted to a value type in cases where the string type is not just numeric, there is no syntax error;
However, when the program runs to actually convert content and assign values, it will error, causing the program to crash
Tip: I don't want to see you, start: #region title ends: #endregion
Arithmetic operators:
+ - * / % ++ --
String type can only use +, the function is to string together;
% take-up
+ + before + +: +1 Before this line code is executed
After + +: Once this line code is executed, then +1
Exercise 1
Please enter your first Student's score:
Please enter a second student's score:
Please enter a third student's score:
The total score of three students is: XX, the average is: xx.
//user InputConsole.Write ("Please enter your first Student's score:"); decimalA =Convert.todecimal (Console.ReadLine ()); Console.Write ("Please enter a second student's score:"); decimalb =Convert.todecimal (Console.ReadLine ()); Console.Write ("Please enter a second student's score:"); decimalc =Convert.todecimal (Console.ReadLine ()); //calculates the sum of scores and the average score decimalD = A + B +C; decimalE = d/3; //PrintConsole.Write ("The total is:"+d+", the average score is:"+e+""); Console.ReadLine ();
Result of Operation
Exercise 2
Please enter the first Student's name:
Please enter the age of the first student:
Please enter your first Student's score:
The first student's name is: XXX, the age is: XXX, the result is: XXX.
Please enter a second student's name:
Please enter a second student's age:
Please enter a second student's score:
The second student's name is: XXX, the age is: XXX, the result is: XXX.
Please enter a third student's name:
Please enter a third student's age:
Please enter a third student's score:
The third student's name is: XXX, the age is: XXX, the result is: XXX.
The names of the three students were: xxx,xxx,xxx.
The ages of three students were: xxx,xxx,xxx, the average age is: XXX.
The total score of three students is: XXX, the average is: XXX.
//user InputConsole.Write ("Please enter the first Student's name:"); stringA1 =Console.ReadLine (); Console.Write ("Please enter the age of the first student:"); decimalB1 =Convert.todecimal (Console.ReadLine ()); Console.Write ("Please enter your first Student's score:"); decimalC1 =Convert.todecimal (Console.ReadLine ()); Console.WriteLine ("The first student's name is:"+ A1 +", The age is:"+ B1 +", the results are:"+ C1 +". "); Console.Write ("Please enter a second student's name:"); stringA2 =Console.ReadLine (); Console.Write ("Please enter a second student's age:"); decimalB2 =Convert.todecimal (Console.ReadLine ()); Console.Write ("Please enter a second student's score:"); decimalC2 =Convert.todecimal (Console.ReadLine ()); Console.WriteLine ("the 21st student's name is:"+ A2 +", The age is:"+ B2 +", the results are:"+ C2 +". "); Console.Write ("Please enter a third student's name:"); stringA3 =Console.ReadLine (); Console.Write ("Please enter a third student's age:"); decimalB3 =Convert.todecimal (Console.ReadLine ()); Console.Write ("Please enter a third academic score:"); decimalC3 =Convert.todecimal (Console.ReadLine ()); Console.WriteLine ("The third student's name is:"+ A3 +", The age is:"+ B3 +", the results are:"+ C3 +". "); //calculates the sum of three students ' scores, the average score and the average age decimalD = c1 + C2 +C3; decimalE = d/3; decimalF = (B1 + b2 + b3)/3; //PrintConsole.WriteLine ("the names of the three students were:"+ A1 +","+ A2 +","+ A3 +". "); Console.WriteLine ("the ages of three students are:"+ B1 +","+ B2 +","+ B3 +", the average age is:"+ F +". "); Console.WriteLine ("The total is:"+ D +", the average score is:"+ E +". "); Console.ReadLine ();
Result of Operation
Data conversion types and arithmetic operators