鱼C论坛

 找回密码
 立即注册
查看: 1306|回复: 5

[已解决]求最小数字,打印输出问题

[复制链接]
发表于 2021-3-15 15:03:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
打印最小的输入数字
我想用int 来定义a,b,c但是最后打印的时候结果一直是0,只有用doble才能正常运行,但是也不能输出小数点的数字
请大神帮忙看下哪个代码有问题,谢谢!



#include<stdio.h>
main(){

    double a,b,c;
    double smallest;
    printf("input the first number\n");
    scanf("%f", &a);

    printf("input the second number\n");
    scanf("%f", &b);

    printf("input the third number\n");
    scanf("%f", &c);

   if(a<b){
       if(a<c){
           a=smallest;
       }else{
           c=smallest;
       }
   }else{
       if(b<c){
           b=smallest;
       }else{
           c=smallest;
       }
   }
    printf("The smallest the number is %d\n",smallest);
   
}
最佳答案
2021-3-15 15:35:23
喜牛牛0101 发表于 2021-3-15 15:34
是的,但是我改为int 和%d 输出就变0了
  1. #include<stdio.h>
  2. main(){

  3.     int a,b,c;
  4.     int smallest;
  5.     printf("input the first number\n");
  6.     scanf("%d", &a);     

  7.     printf("input the second number\n");
  8.     scanf("%d", &b);

  9.     printf("input the third number\n");
  10.     scanf("%d", &c);

  11.    if(a<b){
  12.        if(a<c){
  13.            smallest=a;   //赋值的时候应该把变量放左边
  14.        }else{
  15.            smallest=c;
  16.        }
  17.    }else{
  18.        if(b<c){
  19.            smallest=b;
  20.        }else{
  21.            smallest=b;
  22.        }
  23.    }
  24.     printf("The smallest the number is %d\n",smallest);

  25. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-15 15:27:06 | 显示全部楼层
要求abc都是整数吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-15 15:34:37 | 显示全部楼层
洋洋痒 发表于 2021-3-15 15:27
要求abc都是整数吗

是的,但是我改为int 和%d 输出就变0了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-15 15:35:23 | 显示全部楼层    本楼为最佳答案   
喜牛牛0101 发表于 2021-3-15 15:34
是的,但是我改为int 和%d 输出就变0了
  1. #include<stdio.h>
  2. main(){

  3.     int a,b,c;
  4.     int smallest;
  5.     printf("input the first number\n");
  6.     scanf("%d", &a);     

  7.     printf("input the second number\n");
  8.     scanf("%d", &b);

  9.     printf("input the third number\n");
  10.     scanf("%d", &c);

  11.    if(a<b){
  12.        if(a<c){
  13.            smallest=a;   //赋值的时候应该把变量放左边
  14.        }else{
  15.            smallest=c;
  16.        }
  17.    }else{
  18.        if(b<c){
  19.            smallest=b;
  20.        }else{
  21.            smallest=b;
  22.        }
  23.    }
  24.     printf("The smallest the number is %d\n",smallest);

  25. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-15 15:36:19 | 显示全部楼层
给smallest赋值应该把smallest放在左边
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-15 15:45:04 | 显示全部楼层
洋洋痒 发表于 2021-3-15 15:36
给smallest赋值应该把smallest放在左边

原来是我把赋值的位置搞错了,非常感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-17 18:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表