【问题标题】:Can anyone help me with my C code please ? [closed]任何人都可以帮我写 C 代码吗? [关闭]
【发布时间】:2013-10-05 10:59:41
【问题描述】:

我正在尝试制作欧姆定律程序。 V = 红外。

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int V,I,R; 

    printf("please enter the value of the current if the value is not known make I=0 ");
    scanf("%d", &I);
    printf("please entre the value of the resistance if the value is not known make R=0");
    scanf("%d", &R);
    printf("please enter the value of the voltage, if unknown make V=0");
    scanf("%d", &V);

    if (V == 0) 
        V = I*R;
    {
        printf(" V = %d",V);
    }
    else if (I==0)
        I = V/R;
    {
        printf("I = %d ",I);
    }
    else
        R = V/I; 
    {
        printf("R= %d",R);

    }

    return 0;
}

我是初学者,我怎样才能改进我的代码,让它工作? 非常感谢您的帮助。

【问题讨论】:

  • 欢迎来到stackoverflow。为了帮助您,我们需要知道您的实际问题是什么。您希望该程序做什么以及它会做什么?
  • 请阅读C tutorial。
  • 这个问题似乎离题了,因为它太本地化了。
  • 您使用的是整数。如果将整数 4 除以整数 5,则得到零。
  • 并在if 和else 子句的完整主体周围正确放置{} 括号。

标签: c if-statement floating-point scanf


【解决方案1】:

使用浮点变量:

#include <stdio.h>

int main(int argc, const char * argv[])
{
    float V,I,R; 

    printf("welcome to my lovely program");
    printf("please enter the value of the current if the value is not known make I=0 ");
    scanf("%f", &I);
    printf("please entre the value of the resistance if the value is not known make R=0");
    scanf("%f", &R);
    printf("please enter the value of the voltage, if unknown make V=0");
    scanf("%f", &V);
    if (V == 0)
    {
        V = I*R;
        printf(" V = %f",V);
    }
    else if (I==0)
    {
        I = V/R;
        printf("I = %f ",I);
    }
    else
    {
        R = V/I; 
        printf("R= %f",R);
    }
return 0;
}

如果您使用int 而不是float 或double,则除法时将得到截断值。请学会缩进你的代码——你的 if-else 块都搞砸了。

【讨论】:

    【解决方案2】:

    你需要学习缩进,语句应该在 if /else if /else 块中给出

    if (V == 0) {
       V = I*R;
       printf(" V = %d",V);
    } else if (I == 0) {
       I = V/R;
       printf("I = %d ",I);
    } else {
       R = V/I; 
       printf("R= %d",R);
    }
    

    让你所有的声明都浮动,因为你在计算 I 和 R 时除以第二和第三,因为整数你只会得到整数部分。

    【讨论】:

      【解决方案3】:

      first - 如果在“if”条件中有超过 1 行要执行,则 if 条件后跟括号,因此您应该将第一行放在括号内的 if 条件之后,而不仅仅是打印语句。所有你像 elseif 一样使用的条件语句

      【讨论】:

      • 对于大多数讲英语的人(具有编程背景),“括号”是为“(”和“)”保留的。您所说的“{}”符号将是“大括号”或“大括号”(以区别于“[]”“方括号”)。而且您应该始终使用它们来包围 if 和 else 子句。
      【解决方案4】:

      虽然您可以使用不带括号的 if 语句编写决策,但您所做的(在 if 语句后添加决策,然后放置括号,然后添加另一个决策)在 C 中是不正确的语法。通常,我总是放置决策在括号内的 if 或 else 语句中,因为它使您和其他人更容易阅读和编辑您的代码。因此,要使其工作,请根据上述说明进行以下操作

      #include <stdio.h>
      
      int main(int argc, const char * argv[]) {
          int V,I,R; 
      
          printf("please enter the value of the current if the value is not known make I=0 ");
          scanf("%d", &I);
          printf("please entre the value of the resistance if the value is not known make R=0");
          scanf("%d", &R);
          printf("please enter the value of the voltage, if unknown make V=0");
          scanf("%d", &V);
      
          if (V == 0) 
      
          {
           V = I*R;
              printf(" V = %d",V);
          }
          else if (I==0)
              I = V/R;
          {
              printf("I = %d ",I);
          }
          else
      
          {
          R = V/I;
              printf("R= %d",R);
      
          }
      
          return 0;
      }
      

      但是这只适用于整数,所以如果我是你,我会使用浮点数或双精度数据类型(尽管不要一直使用双精度数据类型,因为它使用的内存比浮点数多,而且你不会t 真的需要一个数字到太多小数位。还有为什么你把参数放在 int main 函数中,而你一次都不使用它们?

      【讨论】:

        猜你喜欢
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        • 2014-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-13
        • 1970-01-01
        相关资源
        最近更新 更多