|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//输入三个整数x,y,z,请把这三个数由小到大输出。
#include <stdio.h>
void compare(int a, int b, int c);
int main()
{
int a, b, c;
printf("please show three numbers:\n ");
scanf("a, b, c", &a, &b, &c);
compare(a, b, c);
return 0;
}
void compare(int a, int b, int c)
{
int t = 0;
if(a>b)
{
t=a; a = b; b = t;
}
if(a>c)
{
t = c; c = a; a = t;
}
if(b>c)
{
t = b; b = c; c = t;
}
printf("the order from smallest to largest is %d %d %d", a, b, c);
}
问题分析:在输入三个整数时,scanf函数的格式字符串应该为"%d %d %d",而不是"a, b, c",这导致了随机值的出现。
修改后的代码如下:
#include <stdio.h>
void compare(int a, int b, int c);
int main()
{
int a, b, c;
printf("please show three numbers:\n ");
scanf("%d %d %d", &a, &b, &c);
compare(a, b, c);
return 0;
}
void compare(int a, int b, int c)
{
int t = 0;
if(a>b)
{
t=a; a = b; b = t;
}
if(a>c)
{
t = c; c = a; a = t;
}
if(b>c)
{
t = b; b = c; c = t;
}
printf("the order from smallest to largest is %d %d %d", a, b, c);
}
|
|