我需要编写一个程序,要求用户提供英寸的总数,并将其值转换为英里,码,英尺和英寸。提示您的用户输入的整数英寸数。您可能会假设他们行为举止良好,并且可以按照说明进行操作,因此请务必为他们提供有关其数量多少的指导。转换它们的值并输出相应的英里数,码数,英尺和英寸,并在输出中标记每个值。例如,如果用户输入
158,430英寸,您的输出应类似于:
158430 inches corresponds to:
2 miles
880 yards
2 feet
6 inches
对于相同的输入值,可能会认为您的输出是正确的,
158430 inches corresponds to:
2 miles
880 yards
1 feet
18 inches
这是我到目前为止所拥有的
#include <stdio.h>
int main(void) {
float inches;
printf("enter number of inches: ");
scanf("%f", &inches);
printf("value in miles is %f", inches/ 63360);
printf("\nvalue in yards is %f", inches/36);
printf("\nvalue in feet is %f", inches/12);
printf("\nvalue in inches is %f", inches);
return 0;
}
答案 0 :(得分:1)
这里没有语法问题。只是逻辑错误。计算单个英里,英尺,英寸,码的方法很好。但是以公制单位的降序查找剩余的方法有点不合逻辑。在计算了以英里,码等为单位的每个值之后,您需要计算还剩下多少英寸。
这是更新的代码:
#include <stdio.h>
int main(void)
{
int inches;
int miles, yards, feet;
printf("enter number of inches: ");
scanf("%d", &inches);
//calculate miles and then find remaining inches
miles = inches/ 63360;
inches = inches - miles*63360;
//calculate remaining yards and then remaining inches
yards = inches/ 36;
inches = inches - yards*36;
//calculate feet and remaining inches after that
feet = inches/ 12;
inches = inches - feet*12;
printf("%d inches corresponds to: ");
printf("\n%d miles", miles);
printf("\n%d yards", yards);
printf("\n%d feet", feet);
printf("\n%d inches", inches);
return 0;
}