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

C语言乘法表实现遇阻,寻求技术解决方案

C语言乘法表程序问题修复方案

代码中的核心问题

  • 语法错误:所有printf的字符串直接换行,未使用C语言标准的换行转义字符\n,这会导致编译失败。C语言字符串内的换行必须通过\n表示,不能直接在字符串里换行。
  • 逻辑不符:注释代码原本要输出1到20的乘法表,但实际for循环条件i < 11只会输出1到10的结果,和预期需求不匹配。
  • 输入无校验:未判断scanf的返回值,若用户输入非整数,程序会出现异常行为。

修正后的完整代码

#include <stdio.h>

int main() {
    int num;
    printf("Enter the number you want multiplication table of: ");
    // 校验输入是否为有效整数
    if (scanf("%d", &num) != 1) {
        printf("Invalid input! Please enter an integer.\n");
        return 1;
    }

    printf("Multiplication table of %d is as follows:\n", num);

    // 循环输出1到20的乘法表
    for (int i = 1; i <= 20; i++) {
        printf("%d X %d = %d\n", num, i, num * i);
    }
    
    return 0;
}

关键修改说明

  1. 替换所有字符串内的直接换行为\n,解决编译报错问题。
  2. 将for循环条件改为i <= 20,实现输出1到20的乘法表,匹配你最初的需求。
  3. 添加scanf返回值校验,当输入无效时给出明确提示并终止程序,避免异常。
  4. 修正输出字符串的拼写错误(as follow → as follows),规范输出文本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 01:15:53