#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,max,max2;
int arr[10];
for(i=0;i<10;i++) arr[i]=rand()%101;
for(i=0;i<10;i++) printf("%d\n",arr[i]);
max=arr[0];
for(i=0;i<10;i++)
{
if(max < arr[i]) max2=max; max=arr[i];
}
printf("Max is : %d\n",max);
printf("2nd max is : %d",max2);
}
输出: 32 32 54 12 52 56 8 30 44 94
最大值:94
第二个最大值是:44
但是当我将“if”语句更改为
if(max < arr[i])
{
max2=max;
max=arr[i];
}
效果很好!
最大值:94
第二个最大值是:56
我以前在单行语句中使用过多个操作,例如
if(xyz) doSomething; break;
但是这里不起作用,为什么?
最佳答案
C 中不存在“单行语句”这样的东西,因为 C 不关心换行符。它们仅用于帮助读者阅读。
这个
if(max < arr[i]) max2=max; max=arr[i];
相当于
if(max < arr[i])
max2=max;
max=arr[i];
相当于
if(max < arr[i])
{
max2=max;
}
max=arr[i];
关于c - 单行语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50314788/