我是C程序的初學者,我試圖創建一個餐館訂單菜單。 我從用戶輸入「Y」開始開始訂購。 然後我想讓程序繼續下訂單,直到用戶輸入「N」停止。 輸入「N」時,將打印總銷售額。 但我不能做循環,你會介意幫助我嗎?謝謝。 :)如何循環直到用戶在C中輸入N
#include <stdio.h>
#include <stdlib.h>
int main()
{
int code;
float totalPrice=0, totalSales = 0 ;
char choice, choice1;
printf("Welcome to Deli Sandwich! Enter Y to start your order!\n");
scanf("%c", &choice);
while(choice=='Y'|| choice=='y')
{
printf("\n____________________________SANDWICH FILLING______________________________\n");
printf("\n\t\t Menu \t\t Code \t\t Price\n");
printf("\n\t\t Egg \t\t 1 \t\t RM 1.00\n");
printf("\n\t\t Tuna \t\t 2 \t\t RM 2.00\n");
printf("\n\t\t Seafood \t 3 \t\t RM 3.00\n");
printf("\n\t\t Chicken Ham \t 4 \t\t RM 2.50\n");
printf("\nSandwich Filling code: ");
scanf("%d", &code);
switch(code)
{
case 1:
printf("Egg is picked.\n");
totalPrice+= 1;
break;
case 2:
printf("Tuna is picked.\n");
totalPrice+= 2;
break;
case 3:
printf("Seafood is picked.\n");
totalPrice+= 3;
break;
case 4:
printf("Chicken Ham is picked.\n");
totalPrice+= 2.50;
break;
default :
printf("invalid code.");
}
printf("\n_____________________________SANDWICH TYPE________________________________\n");
printf("\n\t\t Menu \t\t Code \t\t Price\n");
printf("\n\t\t Half \t\t 1 \t\t RM 3.00\n");
printf("\n\t\t Whole \t\t 2 \t\t RM 5.00\n");
printf("\nSandwich Type code: ");
scanf("%d", &code);
switch(code)
{
case 1:
printf("Half is picked.\n");
totalPrice+= 3;
break;
case 2:
printf("Whole is picked.\n");
totalPrice+= 5;
break;
default :
printf("invalid code.");
}
printf("\nThe total price is RM%.2f.\n", totalPrice);
printf("Thank You. Please come again!\n");
totalSales+= totalPrice;
printf("\nWelcome to Deli Sandwich! Enter Y to start your order!\n");
scanf("%c", &choice);
}
printf("\nThe total sales is RM%.2f.\n", totalSales);
return 0;
}
再次謝謝:)
哦,是嗎?萬分感謝! – njl