C语言:猜数字小游戏

#include<stdio.h>
#include<stdlib.h>
int main(){
int arr[] = { 1, 2, 3, 4, 5 };
int to_find = 4; //假设要求取4的下标
int i = 0;
for (; i < sizeof(arr) / sizeof(arr[0]); i++){ //用来求数组中元素的个数
if (to_find == arr[i]){
break;
}
}
//找到了(i<5)
if (i == 5){
printf(“没有找到预期元素\n”);
}
else{
//找到了
printf(“找到了!下标为 %d\n”, i);
}
system(“pause”);
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44779591/article/details/88897122