#include <stdio.h>
int main() {
char choice;
float x,y;
start:
printf("[c] Converts Celsius -> Fahrenheit\n[f] Converts Fahrenheit -> Celsius\n\n\n");
printf("Enter Choice: ");
scanf("%c",&choice);
if (choice!='c' || choice!='f' || choice!='x') {
printf("Wrong Choice: Try Again!");
goto start;
} if (choice!=x)
printf("Input Value: ");
scanf("%f",&x);
if (choice =='c')
y = 1.8 * x + 32
else
y = (x-32) * (5/9)
printf("Result: %.2f",y);
exit:
return 0;
}
我的老师发布了此内容,但当我尝试时,它有错误,需要帮助来修复它。
最佳答案
这段代码有很多问题:
- 使用
goto
而不是while
循环并不是实现此目的的方法。 - 您的第二个
if
(if (choice != x)
) 缺少大括号。这样,如果为真,则仅执行第一条语句。其余的总是被执行。 - 计算后缺少分号
;
。 - 第一个
if
中的 bool 逻辑不正确。 - 在第二个
if
中,您正在与变量而不是固定值进行比较。 - 您的华氏温度到摄氏度的算术不正确。
希望这些提示对您有所帮助。我不会发布正确的代码,这取决于您,因为您仍在学习;-)。
关于c - 使用 goto 语句 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34976140/