gpt4 book ai didi

c - 执行 while 循环或 while 循环

转载 作者:行者123 更新时间:2023-11-30 21:06:56 27 4
gpt4 key购买 nike

我想听到一些关于我的程序风格的反馈,以及哪个“do while”或“while循环”更好地运行我的程序。另外,如果用户输入无效选项(例如字母),我想在其中放置一个条件。

#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;

}

最佳答案

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

It is more like a while statement, except that it tests the condition at the end of the loop body.

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

关于c - 执行 while 循环或 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46270242/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com