排序3个人的年龄 [英] Sort the age of 3 individuals

查看:120
本文介绍了排序3个人的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/* to find the age of individuals according to youngest to oldest */

#include <stdio.h>

int main(void)
{
  int age1, age2, age3, youngest, middle, oldest;
  {
    printf ("Enter the age of the first individual: ");
    scanf  ("%d", &age1);
    printf ("Enter the age of the second individual: ");
    scanf  ("%d", &age2);
    printf ("Enter the age of the third individual: ");
    scanf  ("%d", &age3);
  }
  if (age1==age2==age3);
  {
    printf("All individuals have the same age of %d", &age1);
  }
  else
  {
    youngest = age1;

    if (age1 > age2)
      youngest = age2;
    if (age2 > age3)
      youngest = age3;

    middle = age1;

    if (age1 > age2)
      middle = age2;
    if (age2 < age3)
      middle = age2;

    oldest = age1;

    if (age1 < age2)
      oldest = age2;
    if (age2 < age3)
      oldest = age3;

    printf("%d is the youngest.\n", youngest);
    printf("%d is the middle.\n", middle);
    printf("%d is the oldest.\n", oldest);
  }
  return 0;
}

我一直在第21行收到错误,声明我有'其他' '用之前的'if'。这里的任何专家都可以告诉我哪里出错了?如果我要删除'else',显示也有点奇怪。

I keep getting the error on line 21 which states that I have an 'else' with a previous 'if'. Any of the experts here can tell me where I went wrong? The display is also a little weird if I were to remove the 'else'.

推荐答案

在你的代码中

 if (age1==age2==age3);

严重受损。

两个主要分数,


  • age1 == age2 == age3 这样的表达式要么


  • 0 == age3 ,当 age1时!= age2

  • 1 == age3 ,当 age1 == age2

  • 0 == age3 , when age1 != age2
  • 1 == age3 , when age1 == age2

这些都不是你想要的。

; 结尾处,如果语句使下一个块无条件。

The ; at the end of the if statement makes the next block unconditional.

充其量,您可以重写

 if ( ( age1 == age2 ) && ( age2 == age3) ) { .... }






之后,如果


After that, in case of

  printf("All individuals have the same age of %d", &age1);

您无需传递地址 - 变量。事实上,这使得语句非常错误,将不兼容的参数类型传递给提供的转换说明符,这会导致 undefined行为

you don't need to pass the address-of the variable. This, in fact makes the statement very wrong, passing an incompatible type of argument to a supplied conversion specifier, which causes undefined behavior.

这篇关于排序3个人的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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