我是C语言编程的新手,我不知道为什么我的程序无法打印所需的输出。
#include <stdio.h>
void main(void)
{
char res,res1;
float money=10;
printf("***Wealcome to Peace of Mind***");
printf("\nHello we have the menu please check::");
printf("\n***Menú***");
printf("\n");
printf("\n<<<Bebidas>>>");
printf("\n 1 - Coca-Cola = 1,5 2 - IceTea = 1,4");
printf("\n 3 - Super Bock = 1,70 4 - Sumol = 1,6");
printf("\n");
scanf("%d",&res);
switch(res)
{
case 1 || 'Coca-Cola':money - CocaCola;break;
}
printf("%.1f",money);
//Is that result i want:
printf("\n%.1f",10-1.5);
}
我的程序的输出:
答案 0 :(得分:2)
您的case
语句的语法不正确。代码还使用scanf()
读取整数,但将整数大小的值存储在char
中。
我整理了代码:
#include <stdio.h>
int main(void)
{
int res;
float cost = 0;
float money = 10;
printf("***Wealcome to Peace of Mind***\n");
printf("Hello we have the menu please check::\n");
printf("***Menú***\n");
printf("\n");
printf("<<<Bebidas>>>\n");
printf(" 1 - Coca-Cola = 1,5 2 - IceTea = 1,4\n");
printf(" 3 - Super Bock = 1,70 4 - Sumol = 1,6\n");
printf("\n");
scanf("%d", &res);
switch(res)
{
case 1:
cost = 1.5;
break;
case 2:
cost = 1.4;
break;
// TODO: case 3 & 4
default:
printf("Invalid Entry\n");
cost = 0;
}
printf("money = %.1f\n", money - cost);
return 0;
}
更多注意事项:
\n
放在字符串的末尾case
块中,最好有一个default
来捕获错误#define
常量(或存储为值数组或某个公共区域,因此该值仅在程序中设置一次,其他所有内容都引用该值)将是值得的。
#define COLA_COST 1.5