【问题标题】:Adding multiplication to a IF Statement将乘法添加到 IF 语句
【发布时间】:2014-10-15 12:01:51
【问题描述】:

我只有一个简短的问题。我将如何在每个 if 语句行中引入乘法,以便将 Height * Width * Depth 一起计时以获得体积。我希望为每一行都这样做。我已经插入了我的代码副本,只是想真正添加。

{

int Height;
int Width;
int Depth;


scanf("%d%d%d", &Height, &Width, &Depth );

if(Height == Width && Depth >=0){
printf("It's a square cuboid\nThe volume is %d ");

}

if(Height == Depth && Width >= 0)
printf("It's a square cuboid\n ");

if(Width == Depth && Height >= 0)
printf("It's a square cuboid\n");

if(Height == Width && Height == Depth)
printf("It's a perfect cube\n ");


} 

【问题讨论】:

  • 不清楚你在问什么。如果你想要一个乘法,那么就写一个乘法;)
  • Height * Width * Depth
  • 另外,我希望输出显示在“音量为 %d”的位置,但我如何将 %d 与最终答案相关联?
  • @HadleighGaudreau,我已经在回答中解释过了
  • 如果它是一个完美的立方体并且所有值都 >= 0,那么所有的 printf 语句都会被执行。

标签: c if-statement multiplication statements


【解决方案1】:

将第四个变量声明为volume,然后像这样为乘法赋值

int volume;  //declaration of volume

volume=Height * Width * Depth;   //assigning the result of the multiplication to the variable

printf("It's a ['your shape'] \nThe volume is %d ",volume);

希望您在音量方面不要超出int 的范围。

【讨论】:

    【解决方案2】:

    你的问题有点不清楚,看看我是否理解正确..

    您还想在每个if 语句条件中检查卷..

    你可以这样做

    if( (Height * Depth * Width) == YOUR CONDITION )
        printf("WHATEVER YOU WANT\n ");
    

    或者,声明另一个变量并在其中分配值,然后在您的 if 语句中使用它

    volume = Height * Depth * Width;
    if( volume == )
        //whatever you want
    

    【讨论】:

      【解决方案3】:

      虽然你会得到一些负面影响,但这里是你应该如何编写你的 printif 语句。如果需要,将其复制到其余的 if 语句中。

      if(Height == Width && Depth >=0){
      printf("It's a square cuboid\nThe volume is %d ", (Height * Width * Depth));
      
      }
      

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        你的问题不清楚。如果你想乘,只需在需要的地方输入Height*Width*Depth。如果您希望它处于if 的条件下,请使用

        if((Height*Width*Depth)==somevalue)
        //Do something
        

        但是,我认为您想使用下面的printf 显示音量:

        printf("It's a square cuboid\nThe volume is %d ");
        

        如果是这样,请使用

        printf("It's a square cuboid\nThe volume is %d ",Height*Width*Depth);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-02
          • 1970-01-01
          相关资源
          最近更新 更多