我正在编写一个代码,该代码在计算重量和里程后为用户提供其包裹的总数。它在工作之前就可以使用,但是在我进行了较小的更改之后,任何重量超过2磅的东西都无法打印出来。为什么会这样?
#include <stdio.h>
#include <stdlib.h>
int main(){
int weight;
int miles;
double mileCost;
int segment;
int remainder;
printf("Charge by weight:(We don't tale packages over 10lbs\n");
printf("\n 1-2 lbs: $1.50\n 3-6 lbs: $3.70\n 7-10 lbs: $5.25\n ");
printf("Enter your package's weight:\n");
scanf("%d", &weight);
printf("Charge by mile: \n");
printf("$1.50 for every 500 miles\n");
printf("Enter the total miles for your package:\n");
scanf("%d", &miles);
if(miles == 0 && weight == 0 && weight < 10){
printf("Invalid entry! Try Again.");
}
segment= miles / 500;
remainder = miles % 500;
if(remainder > 0)
remainder = 1;
if(weight <= 2){
mileCost = 1.50 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
if(weight > 2 && weight >= 6){
mileCost = 3.70 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
if(weight > 6 && weight >= 10){
mileCost = 5.25 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
return 0;
}
答案 0 :(得分:2)
您的上限写为大于或等于<=
,而不是小于或等于if(weight > 2 && weight >= 6){
if(weight > 6 && weight >= 10){
:
if(weight > 2 && weight <= 6){
if(weight > 6 && weight <= 10){
应该是
{{1}}
答案 1 :(得分:1)
关于:
if(weight > 2 && weight >= 6){
1)应该以{{1}}开头,因此如果体重<= 2甚至都不会看
2)声明应为:
else
注意else if(weight > 2 && weight <= 6){
3)存在以下类似考虑因素:
<= 6