关于三个数字从小到大排序的问题
本帖最后由 jpan1221 于 2020-11-15 17:17 编辑代码如下:
include <stdio.h>
int main() {
int a, b, c;
printf("input the first integer number: ");
scanf("%d", &a);
getchar();
printf("input the second integer number: ");
scanf("%d", &b);
getchar();
printf("input the third integer number: ");
scanf("%d", &c);
getchar();
int temp = {0}; int t = 0;
temp = a; temp = b; temp = c;
for(int i = 0; i < 3; i++)
{
for(int j = i + 1; j < 3; j ++)
{
if(temp < temp)
{
t = temp;
temp = temp;
temp = t;
}
}
}
printf("The biggest number is:%d\n",temp);
printf("The smallest number is:%d\n",temp);
}
我没看出来哪里有问题
但是运行结果是这样的:
input the first integer number:10
input the second integer number:1000
input the third integer number:15
The biggest number is:1
000
The smallest number is:0
Process finished with exit code 0
求大佬帮忙看看问题所在 for(int i = 0; i < 3; i++)
{
for(int j = i + 1; j < 3; j ++)
{
if(temp < temp)
{
t = temp;
temp = temp;
temp = t;
}
}
你的错误在这里,0和1比较、交换,1和2比较、交换,最后一次循环2和3比较、交换时,temp是0 1、三个数比较大小,用不着弄得这么复杂;2、输入的都是整数,用不着用 getchar(); 来吸收回车键;
include <stdio.h>
int main() {
int a, b, c,t,max,min;
printf("input the first integer number: ");
scanf("%d", &a);
printf("input the second integer number: ");
scanf("%d", &b);
printf("input the third integer number: ");
scanf("%d", &c);
max = a;
min = a;
if (max < b)
{
max = b;
}
else
{
min = b;
}
if (max < c)
{
max = c;
}
else
{
min = c;
}
printf("The biggest number is:%d\n",max);
printf("The smallest number is:%d\n",min);
}
本帖最后由 jackz007 于 2020-11-15 18:26 编辑
for(int i = 0 ; i < 3 ; i ++)
修改成下面这样
for(int i = 0 ; i < 2 ; i ++) 风过无痕1989 发表于 2020-11-15 17:57
1、三个数比较大小,用不着弄得这么复杂;2、输入的都是整数,用不着用 getchar(); 来吸收回车键;
谢谢1
页:
[1]