ETH官方钱包

切換
舊版
前往
大廳
主題

C語言:成績(jī)輸入器

查理 | 2013-09-07 22:11:29 | 巴幣 0 | 人氣 536



#include <stdio.h>

int main()
{
    int people; //多少人數(shù)
    int grade; //輸入的成績(jī)
    int total; //成績(jī)的總和
    float average; //成績(jī)的平均

    people=0;
    total=0;

    printf("Enter grade ,-1 to end: ");
    scanf("%d",&grade);

//迴圈如果不等於-1則繼續(xù)執(zhí)行
    while(grade!=-1){
        total=total+grade;
        people++;
        printf("Enter grade ,-1 to end: ");
        scanf("%d",&grade);
    }
//如果人數(shù)不等於0,就開始計(jì)算平均
    if(people!=0){
        average = (float)total/people;
        printf("Class average is %.2f\n",average);
    }
    else{
        printf("No grades were entered\n");
    }
    return 0;
}

創(chuàng)作回應(yīng)

更多創(chuàng)作