C # Language Foundation-switch case and if nesting exercises

Source: Internet
Author: User
Tags switch case

Switch Case Learning

Cases:

Console.WriteLine ("1. Hamburgers"); Console.WriteLine ("2. French fries"); Console.WriteLine ("3. Chicken Nuggets"); Console.WriteLine ("4. Chicken Leg"); Console.WriteLine ("5. Chicken Rice Flower"); Console.Write ("Please enter the number of your selected items:"); stringA =Console.ReadLine (); Switch(a) { Case "1": Console.WriteLine ("you chose a hamburger! ");  Break;  Case"2": Console.WriteLine ("your choice is French fries! ");  Break;  Case"3": Console.WriteLine ("you chose the chicken nuggets! ");  Break;  Case "4": Console.WriteLine ("your choice is chicken leg! ");  Break;  Case "5": Console.WriteLine ("your choice is chicken rice flower! ");  Break; default: Console.WriteLine ("wrong input! ");  Break;
}

Switch case is a choice statement

Logical thinking Questions:

Cases:

The judgment is not a leap year, the normal years, is a multiple of 4 but not a multiple of 100
Century the need is a multiple of 400

Enter a year to determine if it is leap years

Console.Write ("Please enter a year:");
int year = Int. Parse (Console.ReadLine ());
if (year >= 0 && year <= 9999)

Console.Write ("Please enter a year:"); intYear =int.            Parse (Console.ReadLine ()); if(Year >=0&& year <=9999)            {                if(Year%4==0&& Year% -!=0) || Year% -==0) {Console.WriteLine ("the year you entered is a leap years! "); }                Else{Console.WriteLine ("the year you entered is common year! "); }            }            Else{Console.WriteLine ("the year you entered is incorrect. "); }

This is a good example, as long as you know what is going on in a leap year.

Example:

Enter month and date to see if the format is correct

There are still mistakes made three times.

Console.Write ("Please enter a year:"); intYear =int.            Parse (Console.ReadLine ()); if(Year >=0&& year <=9999) {Console.Write ("Please enter the month:"); intm =int.                Parse (Console.ReadLine ()); if(M >=1&& m <= A) {Console.Write ("Please enter the date:"); intDay =int.                    Parse (Console.ReadLine ()); if(Day >=1&& Day <= to)                    {                        if(M = =1|| m = =3|| m = =5|| m = =7|| m = =Ten|| m = = A) {Console.WriteLine ("the date you entered is correctly formatted,"+year+""+m+""+Day ); }                        if(m==4|| m==6|| m==9|| m== One)                        {                            if(day<= -) {Console.WriteLine ("the date you entered is correctly formatted,"+year+""+m+""+Day ); }                            Else{Console.WriteLine ("you entered the wrong date! "); }                        }                        Else//the rest of the February                        {                            if(year%4==0&&year% -!=0|| year% -==0)//the case of a leap year                            {                                if(day<= in) {Console.WriteLine ("the date you entered is correctly formatted,"+year+""+m+""+Day ); }                                Else{Console.WriteLine ("the date you entered is in the wrong format! "); }                            }                            Else//Common year                            {                                 if(day<= -) {Console.WriteLine ("the date you entered is correctly formatted,"+year+""+m+""+Day ); }                                Else{Console.WriteLine ("the date you entered is in the wrong format! "); }                            }                        }                    }                    Else{Console.WriteLine ("you entered the wrong date! "); }                }                Else{Console.WriteLine ("you entered the month of all kinds of wrong! "); }                                 }                             Else{Console.WriteLine ("the year you entered is not in the correct format! "); } console.readline ();

The existing logic is still relatively clear, to hit the code always do not know how to mess up, which brackets contain which is disorderly set. We have to continue to improve ourselves.

C # Language Foundation-switch case and if nesting exercises

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.