You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在switch-case中使用数组?C语言作业程序无法执行排序选项求助

问题分析与解决方案

看起来你遇到了两个核心问题:switch-case匹配失败,以及排序后原数组值丢失,再加上代码里的语法错误,咱们一步步来解决:

1. 为什么选项1无法触发?

你用scanf("%d", &option);读取的是整数类型的选项,但switch里写的是case '1':——这里的'1'是字符常量,它的ASCII值是49,和你输入的整数1完全不是一回事!所以输入1的时候,没有匹配的case,程序直接执行switch之后的代码(如果没写default的话就直接退出了)。

解决方法:把case里的字符改成整数,也就是case 1:,去掉单引号。

2. 如何保留数组原有值?

你现在的排序是直接在原数组a上操作的,排序后原数组的顺序就被覆盖了。要保留原始值,你需要创建一个数组副本,对副本进行排序操作,原数组不动。比如:

// 复制A1到临时数组
int a_sorted[10];
for(int i=0; i<10; i++){
    a_sorted[i] = a[i];
}
// 对a_sorted排序,原数组a不受影响

3. 代码语法错误修正

你提供的代码大括号严重不匹配,排序的嵌套for循环之后的printf块没有正确闭合,导致逻辑混乱。必须把每个代码块的大括号配对好。

修正后的完整示例代码

#include <stdio.h>

int main() {
    int a[10], b[10];
    int option;

    // 输入A1数组
    printf("Please Enter 10 Values For A1: ");
    for(int i=0; i<10; i++){
        scanf("%d", &a[i]);
    }
    printf("10 Values Chosen For A1: ");
    for(int i=0; i<10; i++){
        printf(" %d ", a[i]);
    }
    printf("\n");

    // 输入B1数组
    printf("Please Enter 10 Values For B1: ");
    for(int i=0; i<10; i++){
        scanf("%d", &b[i]);
    }
    printf("10 Values Chosen For B1: ");
    for(int i=0; i<10; i++){
        printf(" %d ", b[i]);
    }
    printf("\n");

    // 打印菜单
    printf("Please choose one of the following options:\n");
    printf("* 1: Sort A1 values in Ascending Order\n");
    printf("* 2: Sort B1 values into Descending Order\n");
    printf("* 3: Compute the Sum of the Elements of A1 and B1\n");
    printf("* 4: Subtract the Elements of B1 from the Elements of A1\n");
    printf("* 5: Terminate the program (Exit)\n");
    printf("Enter your option: ");
    scanf("%d", &option);

    switch(option){
        case 1: { // 用大括号包裹case内的变量声明,避免编译问题
            int a_sorted[10];
            // 复制原数组
            for(int i=0; i<10; i++){
                a_sorted[i] = a[i];
            }
            // 升序排序
            for(int i=0; i<10; i++){
                for(int x=i+1; x<10; x++){
                    if(a_sorted[x] < a_sorted[i]){
                        int temp = a_sorted[i];
                        a_sorted[i] = a_sorted[x];
                        a_sorted[x] = temp;
                    }
                }
            }
            // 打印排序后的结果
            printf("\nAscending Order For A1: ");
            for(int i=0; i<10; i++){
                printf(" %d ", a_sorted[i]);
            }
            printf("\n");
            break; // 别忘了加break,否则会继续执行后面的case
        }
        case 2: {
            // 类似case1,创建B1的副本,然后降序排序
            int b_sorted[10];
            for(int i=0; i<10; i++){
                b_sorted[i] = b[i];
            }
            for(int i=0; i<10; i++){
                for(int x=i+1; x<10; x++){
                    if(b_sorted[x] > b_sorted[i]){
                        int temp = b_sorted[i];
                        b_sorted[i] = b_sorted[x];
                        b_sorted[x] = temp;
                    }
                }
            }
            printf("\nDescending Order For B1: ");
            for(int i=0; i<10; i++){
                printf(" %d ", b_sorted[i]);
            }
            printf("\n");
            break;
        }
        case 3: {
            int sum_a = 0, sum_b = 0;
            for(int i=0; i<10; i++){
                sum_a += a[i];
                sum_b += b[i];
            }
            printf("\nSum of A1 elements: %d\n", sum_a);
            printf("Sum of B1 elements: %d\n", sum_b);
            printf("Total sum of A1 and B1: %d\n", sum_a + sum_b);
            break;
        }
        case 4: {
            printf("\nSubtract B1 elements from A1 (A1[i] - B1[i]): ");
            for(int i=0; i<10; i++){
                printf(" %d ", a[i] - b[i]);
            }
            printf("\n");
            break;
        }
        case 5:
            printf("Exiting program...\n");
            break;
        default:
            printf("Invalid option! Please enter a number between 1-5.\n");
    }

    return 0;
}

额外注意点

  • 每个case分支末尾一定要加break;,否则程序会“贯穿”到下一个case执行,这是switch-case的常见坑。
  • 当在case里声明变量(比如int a_sorted[10];)时,要用大括号把整个case的代码包裹起来,避免变量作用域冲突导致编译错误。
  • 现在排序操作是在副本数组上进行的,原数组a和b的原始值会被保留,你可以在后续操作中继续使用。

内容的提问来源于stack exchange,提问作者Jay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:50:17