c - 使用 goto 语句 for 循环

标签 c goto temperature

#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;
}

我的老师发布了此内容,但当我尝试时,它有错误,需要帮助来修复它。

最佳答案

这段代码有很多问题:

  1. 使用 goto 而不是 while 循环并不是实现此目的的方法。
  2. 您的第二个 if (if (choice != x)) 缺少大括号。这样,如果为真,则仅执行第一条语句。其余的总是被执行。
  3. 计算后缺少分号 ;
  4. 第一个 if 中的 bool 逻辑不正确。
  5. 在第二个 if 中,您正在与变量而不是固定值进行比较。
  6. 您的华氏温度到摄氏度的算术不正确。

希望这些提示对您有所帮助。我不会发布正确的代码,这取决于您,因为您仍在学习;-)。

关于c - 使用 goto 语句 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34976140/

相关文章:

c - 使用 pthread_exit() 访问返回值

c++ - 与调用函数相比,goto 语句是否有效?

C用于带有8051微 Controller 的红外温度传感器?

arduino - 如何使用带有 PT100 RTD 传感器的 arduino uno 板读取温度?

C编程-温度转换问题

c - 如何在网络上对 float 进行序列化?

在 Linux 上使用 C 检查目录是否为空

c - 如何修复嵌套在 if-else 中的 while 循环?

c++ - 在没有 goto 和提前返回的情况下在 C++ 中执行多个相关错误检查的优雅方法?

java - Java中有goto语句吗?