The glorious Years of-c# basic Chapter three flow control 2

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.text;namespace P01ReviewAndWhile{class            program {static void Main (string[] args) {//reg ();            Mulloop ();        Console.ReadLine ();                      } #region 9.2 Multi-cycle exercise: Register void Reg ()//9.2 Multi-cycle exercise: Register, ask the user to fill in the user name, if the user name is wrong, prompt the user name error, and asked to re-enter the user name;         Require user to fill in user password, if the user password is wrong, prompt user password error, and require re-enter user password;//Error condition: 1. Cannot be empty; 2. User name and password cannot be the same;            static void Reg () {String strName = "";            String strpwd = ""; while (true) {//1. Loop Check user input name cannot be empty do {Console .                    WriteLine ("Please enter user name:");                    StrName = Console.ReadLine (); if (StrName = = "") {Console.WriteLine ("Username cannot be empty, please re-enter!)                        ");                    Console.WriteLine ("**************************");       }         } while (StrName = = "");                    2. Loop Check user input password cannot be empty do {Console.WriteLine ("Please enter user password:");                    Strpwd = Console.ReadLine (); if (strpwd = = "") {Console.WriteLine ("User password cannot be empty, please re-enter!")                        ");                    Console.WriteLine ("**************************");                }} while (strpwd = = ""); 3. Ask the user if you want to continue to register Console.WriteLine ("Registration is successful ~ ~ Do you want to continue to register it?")                (y/n) ");                String striscontinue = Console.ReadLine ();                if (striscontinue! = "Y") {break;            } Console.WriteLine ("**************************"); } Console.WriteLine ("Exit Successful ~~~!        ");        } #endregion #region 9.1 multi-loop exit void Mulloop ()//<summary>///9.1 Multi-cycle exit </summary> static void Mulloop ()        {for (int i = 0, i < 5; i++) {for (int j = 0; J < 3; J + +)                    {Console.Write ("☆");                        if (j = = 1) {i = 4;//if you want to "exit the external cycle", you can only directly meet the external cycle cycle conditions!                 break;//Note: The break here is to exit the nearest loop (that is, the loop inside)}} Console.WriteLine ("");            Console.WriteLine ("****************************"); }} #endregion #region 9 multi-cycle exercises to hit the specified width of the rectangle///<summary>///9 multi-cycle exercises to hit the finger High width rectangle///</summary> static void Printrec () {Console.WriteLine ("Please enter the height of the rectangle you want to hit:            "); int height = Int.            Parse (Console.ReadLine ());            Console.WriteLine ("Please enter the width of the rectangle you want to play:"); int width = Int.            Parse (Console.ReadLine ()); Multiple loops (nested loops)//1. Print lines (print height rows according to height entered by user) For (int row = 0; row < height; row++) {//2. Prints the stars in each line (the width of a star is printed according to the width of the user input)                for (int low = 0, low < width; low++) {Console.Write ("☆");            //1.1 Prints a newline console.write ("\ n") for each line of stars printed out;        }} #endregion #region exercises require a line on the console screen ************************//<summary>            Exercises require a line to be typed on the console screen ************************//</summary> static void Printstar () {            1. First accept the number of stars to be hit by the user Console.WriteLine ("Please enter the number of stars you want to hit:"); int num = Int.            Parse (Console.ReadLine ());            2. Number of cycles required, 1 * for Each loop (int i = 0; i < num; i++) {Console.Write ("*"); }} #endregion #region 8.0 while loop void while ()///<summary>//8.0 WHI Le cycle///</summary> StatIC void while () {///Identity bit: Identifies whether the user is logged on successfully ~ ~ BOOL IsOk = false; 1.            The while loop is first judged and executed only when the condition is met!                                 while (true), the reason that the loop body code//while (!isok) is reversed is that the loop body is executed only if the Isok=false representative is not logged in; But the while (false) does not execute the loop body, so a while (!false) is required, which is equivalent to while (true) before the loop body code is executed while (!isok)//If the user is not logged into            Execute the Loop body code!                {Console.WriteLine ("Please enter user name:");                String strName = Console.ReadLine ();                Console.WriteLine ("Please enter your password:");                String strpwd = Console.ReadLine ();                    if (StrName = = "Admin" && strpwd = = "123") {Console.WriteLine ("Login Success ~ ~ ~ ~ ~ ~");                IsOk = true; }}} #endregion #region 7.0 Do While loop///<summary>//7.0 do WHI Le Loop///</summary> static void DoWhile () {int i =0;            Features of the 1.0 do While loop: no matter what the conditions are, do it first! Do {//1.1 to 5th, take a break if (i = = 4) {Consol                    E.writeline ("Rest ~ ~ ~ ~ ~ ~ ~");                    i++;                Continue }//1.2 do 19th, do not go, stop the loop if (i = =) {Console.writel                    INE ("Can't Do it anymore ~ ~ ~ ~ ~ ~");                Break                } Console.WriteLine ("Do the first" {0} "sit-ups ~ ~ ~", i + 1);            i++;  } while (I <),//while must be a bool or bool conditional expression and, if true, continue the loop, or, if False, exit the loop//for (int a = 0; A < 20;            a++)//{//Console.WriteLine ("Do the first" {0} "sit-ups ~ ~ ~", a + 1);        }} #endregion #region 6.0 for loop void Forloop ()///<summary>//6.0 cycles    </summary> static void Forloop () {/* 6.1for loop: Specifies the number of times to loop         * Execution Order: * A. First execution, create variable int times = 0 * b. Judging condition times <= 5 if condition is met (TRUE) *             B1. Execution method Body Code * b2. Execution condition Value change times++ * C. Repeat B, knowing that the condition is not satisfied (false), exit the loop */for (int times = 0; times <= 5; times++) {//console.writeline ("teacher, I ran a            Circle ~ ~ ~ "); }//6.1.1 break jumps out of the loop//for (int i = 0; i <; i++)//{//Console.wri            Teline ("I ate {0} buns", i + 1); if (i = = 4)//{//Console.WriteLine ("Ate 5 buns, the 6th one really can't eat ~~~!            ");  break;//exits this layer loop, and when the code runs to break, it automatically exits the loop closest to break//}//}//6.1.2 continue jumps out                    This cycle for (int i = 0; i <; i++) {if (i = = 4) { Console.WriteLine ("Test ~ 5th steamed bun has half a worm ~~~! Throw It away!                    "); continue;//exit this cycle, after continue codeThe face of the non-execution, directly into the next cycle, (jump directly to the i++ go!)            } Console.WriteLine ("I ate {0} buns", i + 1); }} #endregion #region 6.1 topic: Require user input age (0-100)//<summary>//topic: Require user input age (0- 100), if the condition is not met, then ask for re-entry (a total of 3 re-entry opportunities, if not successful, then quit the program!) If successful, displays the user's age//</summary> static void Loopforex () {/* and then look back at "loops": repeating when certain conditions are met Do the same thing * *//1. Use for Loop 3 times for (int i = 0; i < 3; i++) {C Onsole.                WriteLine ("Please enter your Age:"); int age = Int.                Parse (Console.ReadLine ()); if (Ages > 0 && Age <=) {Console.WriteLine ("You are the normal value ~ ~ ~ should be the earth person ~~!                    : 0 "); i = 2;//here because we have received the age of the eligible, so there is no need to recycle;//We will change the loop condition I to 2, then execute the i++ in the For loop and then determine if I <3, because i=i+1 =3, so the condition is not met, and finally exits the loop directly} else {Console.wriTeline ("Your age is a bit strange ~ ~ ~ Normal person is 0-100, please come back again ~ ~ ~"); }}} #endregion #region 6.2 Cycle Title: Login Exercise +void Login ()//<summary>//    /6.2 Cycle Title: Login Exercises//</summary> static void login () {/*6.2 cycle title: Login Exercise * A. Receive the user name and password, if the user name and password equals admin and 123, prompt login success, otherwise always prompt login failed * b. After the login is successful, the user is required to enter the deposit amount in their bank. * C. Requirements             User input age, if the age of 0-20, to users bonus: Deposit amount * 0.2 * between 21-30, issued to the user bonus: Deposit amount * 0.4 * Between 31-100, send user bonus: Deposit amount * 0.6 * Note: Age must be between 0-100 and the whole            Number */String strName = "";            String strpwd = ""; 1. Receive user name and password//for (;;)                Note: for (), the number cannot be omitted, others can be omitted while (true) {Console.WriteLine ("Please enter your user name:");                StrName = Console.ReadLine ();      Console.WriteLine ("Please enter your password:");          Strpwd = Console.ReadLine ();                    if (StrName = = "Admin" && strpwd = = "123") {Console.WriteLine ("Login Successful ~ ~ ~"); break;//Exit Loop} else {Console.WriteLine ("Username or password is incorrect, please reenter ~"                    );                Console.WriteLine ("----------------------------");            }}//2. Enter Age Console.WriteLine ("Please enter your Age:");            int age = Convert.ToInt32 (Console.ReadLine ());            3. Enter your existing deposit amount Console.WriteLine ("Please enter your deposit amount, we will give you the corresponding bonus:"); Decimal money = Decimal.            Parse (Console.ReadLine ());            4. Grant different bonuses according to age decimal Jiangjin = 0m;            if (age > 0 && Age <=) {Jiangjin = money * 0.2M;            } else if (age > && age <=) {Jiangjin = money * 0.4M; } else if (age > && age <=) {Jiangjin = money * 0.6M;        } Console.WriteLine ("Congratulations on your winning the" {0} "Bonus ~ ~ ~", Jiangjin);        } #endregion #region 5.0 ternary operator?: +void Threeyuanope ()///<summary>//5.0 ternary operator?:            </summary> static void Threeyuanope () {Console.WriteLine ("Please enter your dog's age:");            int age = Convert.ToInt32 (Console.ReadLine ());            String hornorname = "";            if (age >) {hornorname = "hero old dog ~ ~";            } else {hornorname = "Puppy dog ~ ~";            } Console.WriteLine ("Dog Honor Name:" + hornorname); /* Ternary operator: *?                            The number must be preceded by a bool expression (an expression that ultimately returns a bool value) * If the bool expression above returns True, then return the value preceding the: number to the previous variable honorName2 * False, then return the value following the: Number * Note: Can be used to replace the simple if else */StrinG HonorName2 = age > All && Age < 22?        "Hero Old Dog": "Puppy Dog"; } #endregion #region 4.0 commonly used string escape characters///<summary>///4.0 commonly used string escape characters///</summ ary> static void Transferchar () {//1. quote transfer character \ \ n line-break transfer character \ t backspace \ \ Backslash Escape (output one \ character) s Tring STRMSG = "\ t small white, i \" Love "you ~!\n you believe \\n?            ~~";        Console.WriteLine (STRMSG);        } #endregion #region 3.0 logical operator void logicoperation ()//<summary>//3.0 logical operators            </summary> static void Logicoperation () {Console.WriteLine ("Please enter your Age:"); int age = Int.            Parse (Console.ReadLine ()); if (age > 0)//{//if (age < +)//{//Console.Write Line ("Your age is normal, congratulations you come to the sinful Earth ~~~!            ");            Console.WriteLine (age);            }//}//1.0 logic and && for connecting two expressions with a bool valueExecution order: From left to right, once an expression is encountered with a bool value of false, then the whole is false, and the subsequent expression is no longer executed//if Age =-1, then age>0=false,&am The expression after the p;& symbol is no longer executed!                      Most importantly: The result of the entire && expression is false//&& The return value of the expression: Returns true only if all judgments in the entire expression are true                 Any one of them evaluates to false, then the entire expression returns False if (age > 0 && Age < 110) { Console.WriteLine ("Your age is normal, congratulations you come to the sinful Earth ~~~!                ");            Console.WriteLine (age); }//2.0 logic or | | : Any judgment is true, the entire expression returns TRUE//only returns False if all judgments in the entire form are false if (age < 1 0 | |            Age >) {Console.WriteLine ("Because you are <10 or >80 years old, so you do not need to buy tickets ~ ~ ~");            }//3.0 Reverse!            bool isOk =!false;            Console.WriteLine ("isok=" + isOk); 3.1 If the and = number is connected, it is called the Not equal! =; Note: no greater than!> < if (age! = 0) {} if (! ( Age >) {} if (!isok)//If isok=false, then!isok = True to enter into the IF statement {            Console.WriteLine ("Haha haha ~ ~ ~");            } if (True) {Console.WriteLine ("123123"); }} #endregion #region 1.0 swap variables//<summary>//1.0 Swap variables///</summa            ry> static void M01swiftvar () {int a = 110;            int b = 120;            1.1 Define intermediate variable int x = a;//The value of one of the variables to be exchanged is stored in x a = B;            b = x;//The value in X, the exchange algorithm of B//1.2 value Type a = a + b;//110 + 120=230 B = a-b;//230-120 =110        A = a-b;//230-110 =120} #endregion #region 2.0 self-increment ...//<summary>            2.0 self-increase self-reduction ...///</summary> static void M02self () {int a = 1;            A = a + 1;   2.1 Self-increment a++ is equivalent to a=a+1 a++;         2.2 Self-reducing a--;//a=a-1;            2.3 The value specified by the self-operation int b = 2;            B + = 220;//equivalent: b = b + 220;            B-= 220;//equivalent to: b = b-220;            b *= 2;//equivalent: b = b * 2;            b/= 2;//equivalent: b = b/2;            b%= 2;//equivalent: b = b% 2;            2.4 About + + and--the front and rear ************** int x = 1;            A. If a pre-or post-operation is used alone, the result is self +1 x + +;            ++x; B. If combined into an operation or assignment statement, there is a distinct difference between the//B1.            "POST": First "operation or assignment", then + + int z = 1;            int z2 = z++;//Note: Here First run = number, then run the + +//equivalent://int z2 = Z;            Z = z + 1;            Console.WriteLine ("z2=" + z2);//1 Console.WriteLine ("z=" + z);//2 int z3 = 2 + z++;            Console.WriteLine ("z3=" + z3);//4 Console.WriteLine ("z=" + z);//2//b2. "Front": Advanced line + + again "operation or assignment"            int o = 1;            int O2 = ++o;//O2 = 1+o;    Console.WriteLine ("o2=" + O2);//2        int O3 = 2 + ++o; Console.WriteLine ("o3=" + O3);//5//b3.            Mix, please be sure to note the priority of the symbol: Pre > operation and Assignment > post int x2 = 1; int O4 = x2++ + 3 + ++x2;  2 + 3 + 2/* Process Order: * 1.  ++X2, x2=2 * 2.  2 + 3 + 2 * 3.  O4 = 7 * 4.        x2++, x2 =3 */Console.WriteLine ("o4=" + O4); } #endregion}}

The glorious Years of-c# basic Chapter three flow control 2

Related Article

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.