给定四种商品,分别是薯片(crisps)、爆米花(popcorn)、巧克力(chocolate)和可乐(cola),单价分别对应为3.0元/公斤、2.5元/公斤、4.0元/公斤、3.5元/份。
首先在屏幕上显示以下菜单:
[1] crisps
[2] popcorn
[3] chocolate
[4] cola
[0] exit
用户可以输入编号1~4查询对应食品的单价。当连续查询次数超过5次时,程序应自动退出查询;不到5次而用户输入0即退出;输入其他编号,显示价格为0。
输入格式:
输入在一行中给出用户连续输入的若干个编号。
输出格式:
首先在屏幕上显示菜单。然后对应用户的每个输入,在一行中按格式“price = 价格”输出查询结果,其中价格保留一位小数。当用户连续查询次数超过5次、或主动输入0时,程序结束,显示“Thanks”。
输入样例1:
1 7 0 2
结尾无空行
输出样例1:
[1] crisps
[2] popcorn
[3] chocolate
[4] cola
[0] exit
price = 3.0
price = 0.0
Thanks结尾无空行
输入样例2:
1 2 3 3 4 4 5 6 7 8
结尾无空行
输出样例2:
[1] crisps
[2] popcorn
[3] chocolate
[4] cola
[0] exit
price = 3.0
price = 2.5
price = 4.0
price = 4.0
price = 3.5
Thanks结尾无空行
answer
#include <stdio.h>
int main(){
double price[4] = {3.0,2.5,4.0,3.5};
int number;
printf("[1] crisps\n[2] popcorn\n[3] chocolate\n[4] cola\n[0] exit\n");
for(int i = 0;i < 5;i++) {
scanf("%d",&number);
if(number == 0)
break;
else if(number == 1)
printf("price = %.1lf\n",price[number-1]);
else if(number == 2)
printf("price = %.1lf\n",price[number-1]);
else if(number == 3)
printf("price = %.1lf\n",price[number-1]);
else if(number == 4)
printf("price = %.1lf\n",price[number-1]);
else
printf("price = 0.0\n");
}
printf("Thanks");
return 0;
}