One, Operator:
(i), relational operators: > < >= <= = = =!
1, bool a = ten > 5; The result of the relationship comparison, the type must be bool,
Must be received with a variable of type bool.
(ii), logical operators
1. && and:
Example: 10<11 && 10>9:true, 10<11 && 10<9:false
PS: and relationship: As long as there is one that is not true, then return False,
If you want to return true, then it must all be set up
2, | | Or:
Example: 10<11 | | 10<9:true, 10<9 | | 10>11:false
PS: or relationship: As long as there is a set, then return True,
If you want to return false, you must not have it all
3,! Non: Apology
(iii), conditional operators
Data Type A = comparison expression? Value: value;
For example: a = 10;
b = 15;
String a = (a > B)? "Yes": "Wrong";
Exercises
1.
Please enter your name:
Please enter gender (1/0):
My name is xxx, I am xxx.
//receiving information entered by the userConsole.Write ("Please enter your name:"); stringName =Console.ReadLine (); Console.Write ("Please enter gender (1/0):"); intSex =Convert.ToInt32 (Console.ReadLine ()); //calculate a comparison to derive a variable stringx = Sex = =1?"male":"female"; //Print OutputConsole.WriteLine ("My name"+ name +", I was"+ x +"the. "); Console.ReadLine ();
2.
Please enter your name:
Please enter your age:
If it is greater than or equal to 18 years old, the output: My name is xxx, I am an adult!
If it is less than 18 years old, then output: My name is xxx, I am also underage.
//receiving information entered by the userConsole.Write ("Please enter your name:"); stringName1 =Console.ReadLine (); Console.Write ("Please enter your age:"); decimalAge1 =Convert.todecimal (Console.ReadLine ()); //compare the derived variables stringA = (Age1 >= -) ?"Adult! ":"still underage. "; //Output PrintingConsole.WriteLine ("My name"+ name1 +", I"+a); Console.ReadLine ();
3.
Please enter an integer between 10-20:
(This number includes 10 and 20)
Output:
The number you entered is: XXX, enter the correct.
Or
The number you entered is: XXX, input error.
//receiving information entered by the userConsole.Write ("Please enter a 10-20-room integer (this number contains 10 and a):"); intA =Convert.ToInt32 (Console.ReadLine ()); //compare the derived variables stringA1 = (A >=Ten&& a <= -) ?", enter correct. ":", enter an error. "; //Output PrintingConsole.WriteLine ("the number you entered is:"+ A +A1); Console.ReadLine ();
4. Please enter an integer between 10-20:
(This number contains 10 and 20, but not equal to 15 and 18)
Output:
The number you entered is: XXX, enter the correct.
Or
The number you entered is: XXX, input error.
//Accept user-entered informationConsole.Write ("Please enter an integer between 10-20: (this number contains 10 and 20, but not equal to 15 and):"); intA =Convert.ToInt32 (Console.ReadLine ()); //compare the derived variables stringb = (a >=Ten&& a <= -&& A! = the&& A! = -) ?", enter correct. ":", enter an error. "; //Output PrintingConsole.WriteLine ("the number you entered is:"+ A +b); Console.ReadLine ();
5.
Please enter an integer between the number 0-20:
Cannot be 0, can be 20.
If not 5, the last increase is not 5. ”;
If 5, then the number is within 10. The end
If 15, the last increment is "not 15." ”;
If 15, then in "is the number between 10-20." The end
Note that punctuation should be correct!
Output:
The number you entered is: XXX, entered correctly, is within 10 of the number, not 5.
Or
The number you entered is: XXX, entered correctly, is a number between 10-20, not 15.
Or
The number you entered is: XXX, input Error!
//receiving information entered by the userConsole.Write ("Please enter an integer between the number 0-20:"); intA =Convert.ToInt32 (Console.ReadLine ()); //compare the derived variables stringb = (A >0&& a <= -) ?", enter the correct":", input error"; stringc = (A >0&& a <=Ten) ?", is within 10 of the number":""; stringD = (A >Ten&& a <= -) ?", which is a number between 10-20":""; stringE = (A >0&& a <=Ten&& A! =5) ?", not for 5":""; stringf = (A >Ten&& a <= -&& A! = the) ?", not for the":""; //Output PrintingConsole.WriteLine ("the number you entered is:"+ A + B + C + D + E + f +". "); Console.ReadLine ();
Relationships, logic, conditional operators