c语言从键盘上输入若干个学生的成绩,统计并输出其中的最高成绩和最低成绩,当输入负数时结束输入

2025-02-24 20:13:39
推荐回答(1个)
回答1:

① 代码:

#include

int main(int argc, char const *argv[])
{
    double score, highest, lowest;
    int n;

    highest = -1; lowest = 1000000000;
    n = 0;
    while(1) {
        scanf("%lf", &score);
        if (score < 0.0) break;
        if (highest < score) highest = score;
        if (lowest > score) lowest = score;
        n++;
    }

    printf("Total %d:\n", n);
    printf("  The highest score is : %6.2f\n", highest);
    printf("  The lowest  score is : %6.2f\n", lowest);
    return 0;
}

② 运行:

100 60 92.5 80.5 50.8 -1
Total 5:
  The highest score is : 100.00
  The lowest  score is :  50.80