C简易程序无法正常工作-"if"表示 [英] C easy program not working - "if"

查看:65
本文介绍了C简易程序无法正常工作-"if"表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个简单的程序,该程序比较3个数字并打印出最大的3个数字,但它会继续打印所有3个数字,我不知道为什么.那是我的代码:

I tried to write an easy program that compares 3 numbers and prints the biggest of them but it keeps printing all 3 of them and I don't get why. That's my code:

#include <stdio.h>

int main()
{
  int x = 10;
  int y = 8;
  int z = 3;

  if((x > y) && (x > z));
  {
    printf("%d",x);
  }

  if((y > x) && (y > z));
  {
    printf("%d",y);
  }
  if((z > x) && (z > y));
  {
    printf("%d",z);
  }
  return 0;

}

感谢您的帮助!

推荐答案

在每个if语句的末尾删除分号.这导致if语句运行null语句(;),然后运行块语句{printf(...); }

Remove the semicolon at the end of each if statement. That is causing the if statement to run the null statement (;) and then subsequently run a block statement { printf(...); }

#include <stdio.h>

int main()
{

  int x = 10;
  int y = 8;
  int z = 3;


  if((x > y) && (x > z))
  {
    printf("%d",x);
  }

  if((y > x) && (y > z))
  {
    printf("%d",y);
  }
  if((z > x) && (z > y))
  {
    printf("%d",z);
  }
  return 0;

}

这篇关于C简易程序无法正常工作-"if"表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆