做while循环或while循环

时间:2017-09-18 01:01:07

标签: c

我希望听到一些关于我的计划风格的反馈,哪一个更好?#34;同时"或者" while loop"运行我的程序。 此外,如果用户输入一个无效的选项,如信件,我想放置一个条件。

#include <stdio.h>
#include <stdlib.h>

#define Tequila 20
#define Whiskey 25

int main()
{
    int choice = 0;
    int bottle1= 0;
    int bottle2= 0;
    int final_total= 0;
    int total1 =0;
    int total2 =0;

    do{
        printf("\tMain Menu MK liquor Store\n");
        printf(" \tEnter your choice: \n");
        printf("\t 1-  Buy Tequila Jose Cuervo\n");
        printf("\t 2-  Buy Whiskey Jack Daniels\n");
        printf("\t 3-  Quit\n");

        scanf("%d", &choice);

        switch(choice)
        {
            case 1:
                printf("how many bottles of Tequila would you like\n");
                scanf("%d", &bottle1);
                printf("so far you got %d bottles in your basket \n", bottle1);

                break;
            case 2:
                printf("how many bottles of Whiskey would you like\n");
                scanf("%d", &bottle2);
                printf("so far you got %d bottles in your basket\n", bottle2);


                break;
            case 3:
                total1=bottle1 * Tequila;
                total2= bottle2 * Whiskey;
                final_total = total1+total2;
                printf("You obtained %d bottles of Tequila and %d of Whisky\n", bottle1,bottle2);
                printf("Your total  is %d dollars\n",final_total);

                break;
            default:

                printf("ERROR INVALID OPTION.\n\n");

                break;

        }

    }while (choice != 3);
        return 0;

    }

1 个答案:

答案 0 :(得分:0)

如果你需要它至少运行一次,请使用do-while循环。它通常 那么简单。 TutorialsPoint

  

它更像是一个while语句,除了它测试循环体末尾的条件。

所以请使用一般的经验法则。