#include <stdio.h>

int main() { int arr[10]; int x; int i, count = 0;

// 输入10个整数保存到数组中
for(i=0; i<10; i++) {
    scanf("%d", &arr[i]);
}

// 输入查询的整数x
scanf("%d", &x);

// 查询整数x在数组中出现的位置
for(i=0; i<10; i++) {
    if(arr[i] == x) {
        printf('index is: %3d\n', i);
        count++;
    }
}

// 如果未查询到,输出'Not Found'
if(count == 0) {
    printf("Not Found");
}

return 0;

}

要让这个代码在输出数字前输出一个index is:怎么办

内容:要在输出数字前输出一个'index is:',只需要在printf语句中修改输出格式即可。修改后的代码如下:

#include <stdio.h>

int main() {
    int arr[10];
    int x;
    int i, count = 0;

    // 输入10个整数保存到数组中
    for(i=0; i<10; i++) {
        scanf("%d", &arr[i]);
    }

    // 输入查询的整数x
    scanf("%d", &x);

    // 查询整数x在数组中出现的位置
    for(i=0; i<10; i++) {
        if(arr[i] == x) {
            printf('index is: %3d\n', i);
            count++;
        }
    }

    // 如果未查询到,输出'Not Found'
    if(count == 0) {
        printf("Not Found");
    }

    return 0;
}

这样修改后,输出的结果将会是每个数字前会有'index is:'的提示。


原文地址: https://www.cveoy.top/t/topic/pmHc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录