所以这就是问题:
用c写一个程序,它读取盘子的数量和盘子的成本,玻璃杯的数量和玻璃的成本,以及客户paid.The程序也需要的总成本来显示变化。
这是我的代码:
#include <stdio.h>
int main(){
float change,total_money;
float glasses,plates;
float cost_plates,cost_glasses,total_cost;
printf("how many glasses you bought: ");//only intenger
scanf("%f",&glasses);
while (glasses<=-1){
printf("Wrong value type again!\n");
printf("how many glasses you bought: ");
scanf("%f",&glasses);
}
printf("how much each glass cost: ");
scanf("%f",&cost_glasses);
while (cost_glasses<=0) {
if(cost_glasses==0)
{
printf("Free? ok!\n");
}
break;
printf("Wrong value type again!\n");
printf("how much each glass cost: ");
scanf("%f",&cost_glasses);
}
printf("How many plates you bought: "); //only integer
scanf("%f",&plates);
while (plates<=-1) {
printf("Wrong value type again!\n");
printf("How many plates you bought: ");
scanf("%f",&plates);
}
printf("how much each plate cost: ");
scanf("%f",&cost_plates);
while (cost_plates<=0) {
if(cost_plates==0)
{
printf("Free ok!\n");
}
break;
printf("Wrong value type again!\n");
printf("how much each plate cost: ");
scanf("%f",&cost_plates);
}
total_cost= (cost_glasses * glasses) + (cost_plates * plates);
printf("how much money you gave:");
scanf("%f",&total_money);
while(total_money<=-1) {
printf("Wrong value type again!\n");
printf("how much money you gave:");
scanf("%f",&total_money);
}
while(total_money<total_cost) {
printf("Thief!!! Type again!\n");
printf("how much money you gave:");
scanf("%f",&total_money);
}
printf("You paid %.2f $ total, the plates and the glasses were costumed %.2f $ so your change was %.2f $",total_money,total_cost,total_money-total_cost);
return 0;
}我知道这是一个糟糕的代码,但我的问题是(看看代码的注释),当用户输入除整数以外的任何其他内容时,我想打印一个错误。
发布于 2020-03-24 17:01:01
老实说,我没有完全听懂你的问题。如果用户没有回答“你买了多少个眼镜(或盘子)”,那么他/她如何以及在哪里确定他/她购买的盘子的数量?
我可以肯定一件事。在break语句之后不执行任何语句和命令。也许你的意思是
while (cost_glasses<=0){
if(cost_glasses==0)
{
printf("Free? ok!\n");
break;
}
printf("Wrong value type again!\n");
printf("how much each glass cost: ");
scanf("%f",&cost_glasses);
}更新
要过滤掉板数的负值或十进制值,请使用
while (glasses<=-1 || int(glasses)!=glasses){
printf("Wrong value type again!\n");
printf("how many glasses you bought: ");
scanf("%f",&glasses);
} 而不是
while (glasses<=-1){
printf("Wrong value type again!\n");
printf("how many glasses you bought: ");
scanf("%f",&glasses);
} https://stackoverflow.com/questions/60827408
复制相似问题