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

C语言使用scanf批量输入整数异常、输入自动跳过问题求助

C语言学生成绩计算程序修复方案

问题根因

你遇到的两个问题都源于同一个错误:所有scanf的格式控制字符串末尾多写了\n。
scanf格式串中的\n属于空白匹配符,它不会匹配输入结束时你按下的回车键,而是要求程序持续吞掉输入流中所有的空白字符(空格、换行、制表符都算),直到遇到第一个非空白字符才会结束本次输入操作,因此会出现必须多输入内容才能继续、后续输入步骤被跳过的现象。

修复步骤

  • 删除所有scanf格式字符串末尾的\n,期末考试输入的scanf前多余的空格也可以一并删除
  • 修正代码中的拼写错误:两处Enter you改为Enter your
  • 修正非法注释:将<This is the code that automatically messes up>改为合法的C语言注释格式

修复后完整代码

#include <stdio.h>

int main()
{
    int testOne, testTwo;
    float totTest, percTest, testPoint;
    printf("Enter your test grades:\n");
    scanf("%d%d",&testOne,&testTwo);
    totTest=testOne+testTwo;
    printf("Your total grade points is: %.1f\n", totTest);
    testPoint=200;
    percTest=(totTest*.40)/testPoint *100;
    printf("Your percentage is %.1f%\n", percTest);
    
    int quizOne, quizTwo, quizThree, quizFour;
    float totQuiz, percQuiz, quizPoint;
    printf("Enter your quiz grades:\n");
    scanf("%d%d%d%d", &quizOne, &quizTwo, &quizThree, &quizFour);
    totQuiz=quizOne+quizTwo+quizThree+quizFour;
    printf("Your total quiz grade is %.1f\n", totQuiz);
    quizPoint=400;
    percQuiz=(totQuiz*.30)/quizPoint *100;
    printf("Your quiz percentage is %.1f%\n", percQuiz);
    
    int examOne, examPoint; // 原问题代码位置
    float percExam, finGrade;
    printf("Enter your exam grade\n");
    scanf("%d", &examOne);
    examPoint=100;
    percExam=(examOne*.30)/examPoint *100;
    printf("Your final exam grade is %.1f\n", percExam);
    finGrade=(percExam+percQuiz+percTest);
    printf("Your overall grade is %.1f%\n", finGrade);
    
    return 0;
}

内容的提问来源于stack exchange,提问作者AntPlays Daily Videos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 23:54:03