首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一个小疑点求解

2012-06-17 
一个小问题求解!#include stdafx.h#include stdio.hvoid max(int a,int b)void main(){int x,yprint

一个小问题求解!
#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;
}

热点排行