今天学习了一个叫做Switch case 的小东西
通俗地来讲就是一个选择箱
可以选择东西

Switch (XXXXX)

简单的来说就是这一个简单的语句
下面用一个小例子来说明
比如说需要做一个菜单

  1. pizza - 45 RMB
  2. croissant - 45 RMB
  3. Dount - 10 RMB
  4. pastry - 15 RMB
  5. bread - 12 RMB
  6. coffer jackry like most - 6 RMB
    来表示不同的东西的不同价格
    用户可以自行输入数字来表示他们需要什么东西
    然后程序就会做一个加法
    将菜品的价格加起来

    #include<stdio.h>
    void main ()
    {
    int choice = 0;
    int total_bill = 0;
    do
    {

     printf ("****************MENU****************\n");
     printf ("1.Pizza                    45 RMB\n");
     printf ("2.Corissant                45 RMB\n");
     printf ("3.Dount                    10 RMB\n");
     printf ("4.Pastry                15 RMB\n");
     printf ("5.Bread                    12 RMB\n");
     printf ("6.coffer jackry like most        6 RMB\n");
     printf ("************************************\n");
     printf ("input 0 to exit or finish\n");
     scanf ("%d",&choice);//i want to use same variable in all conditions
     switch (choice)//check the calue of choice
     {
         case 1: //what to do if the vaule is 1
         {
             total_bill += 45;//total_bill = total_bill + 45
             printf ("½«Pizz¼ÓÈëºÀ»ªÎç²Í\n");
             printf ("------------------------------\n");
             printf ("µ±Ç°×ܽð¶îΪ:");
             printf ("%d\n",total_bill);
             printf ("------------------------------\n");
             break;//break the case ´òÆƺÐ×ÓÌÓ³öÀ´ 
         }//c will do this while the vaule is 1
         case 2:
         {
             total_bill += 45;//total_bill = total_bill + 45
             printf ("½«Corissant¼ÓÈëºÀ»ªÎç²Í\n");
             printf ("------------------------------\n");
             printf ("µ±Ç°×ܽð¶îΪ:");
             printf ("%d\n",total_bill);
             printf ("------------------------------\n");
             break;
         }
         case 3:
         {
             total_bill += 10;//total_bill = total_bill + 45
             printf ("½«Dount¼ÓÈëºÀ»ªÎç²Í\n");
             printf ("------------------------------\n");
             printf ("µ±Ç°×ܽð¶îΪ:");
             printf ("%d\n",total_bill);
             printf ("------------------------------\n");
             break;
         }
         case 4:
         {
             total_bill += 15;//total_bill = total_bill + 45
             printf ("½«Pastry¼ÓÈëºÀ»ªÎç²Í\n");
             printf ("------------------------------\n");
             printf ("µ±Ç°×ܽð¶îΪ:");
             printf ("%d\n",total_bill);
             printf ("------------------------------\n");
             break;
         }
         case 5:
         {
             total_bill += 12;//total_bill = total_bill + 45
             printf ("½«Bread¼ÓÈëºÀ»ªÎç²Í\n");
             printf ("------------------------------\n");
             printf ("µ±Ç°×ܽð¶îΪ:");
             printf ("%d\n",total_bill);
             printf ("------------------------------\n");
             break;
         }
         case 6:
         {
             total_bill += 6;//total_bill = total_bill + 45
             printf ("½«Delicious coffee¼ÓÈëºÀ»ªÎç²Í\n");
             printf ("------------------------------\n");
             printf ("µ±Ç°×ܽð¶îΪ:");
             printf ("%d\n",total_bill);
             printf ("------------------------------\n");
             break;
         }
     }

    }
    while (choice != 0);
    printf ("µ±Ç°×ܽð¶îΪ:%d\n",total_bill);
    printf ("please choice a way to pay ");
    }

Last modification:August 18, 2020
如果觉得我的文章对你有用,请随意赞赏