原本代码是想实现两个数互换的,但是我不知道为什么实现不了,计算机入门新生瑟瑟发抖,请大家帮我解答一下(⋟﹏⋞)
代码如下:
#include<stdio.h>
void swap1(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("%d,%d\n",a,b);
}
void swap2(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("%d,%d\n",a,b);
}
int main()
{
int x,y;
printf("请输入两个整数:\n");
scanf("%d,%d\n",&x,&y);
swap2(x,y);
printf("%d,%d\n",x,y);
}
两处小细节错误,
第一处:输出的整数值,可是上面直接输出了指针变量,是不对的,因此将框中
a,b改为*a,*b
第二处:
#include<stdio.h>
void swap1(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("%d,%d\n",*a,*b);
}
void swap2(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("%d,%d\n",a,b);
}
int main()
{
int x,y;
printf("请输入两个整数:\n");
scanf("%d%d",&x,&y);
swap2(x,y);
printf("%d,%d\n",x,y);
}
scanf()中的换行去掉。
注意:输入的两个数之间用逗号分隔。
如:
3,5
#include<stdio.h>
void swap1(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("%d,%d\n",a,b);
}
void swap2(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("%d,%d\n",a,b);
}
int main()
{
int x,y;
printf("请输入两个整数:\n");
scanf("%d,%d",&x,&y);
swap2(x,y);
printf("%d,%d\n",x,y);
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!