读书人

一个小疑点求解

发布时间: 2012-06-17 21:02:01 作者: rapoo

一个小问题求解!
#include "stdafx.h"
#include "stdio.h"
void max(int a,int b);

void main()
{

int x,y;
printf("请分别输入x和y的值:\n");
scanf("%d%d",&x,&y);
if(x<y)
max(x,y); //这里调用max
printf("%d %d\n",x,y);
}
void max(int a,int b) //max实现2个数字互换
{

int temp;
if(a<b)
temp=a;
a=b;
b=temp;
}

不知道为什么,我输入数字,比如输入2和5,输出也是2和5,没有实现大数字在前的目的!
不知道问题出在哪里,求解!

[解决办法]
把void max(int a,int b); 改成 void max(int &a,int &b);
#include "stdio.h"
void max(int &a,int &b);
void main()
{

int x,y;
printf("请分别输入x和y的值:\n");
scanf("%d%d",&x,&y);
if(x<y)
max(x,y); //这里调用max
printf("%d %d\n",x,y);
}
void max(int &a,int &b) //max实现2个数字互换
{

int temp;
if(a<b)
temp=a;
a=b;
b=temp;
}

读书人网 >C++

热点推荐