ETH官方钱包

切換
舊版
前往
大廳
主題

C語言:成績輸入器

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



#include <stdio.h>

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

    people=0;
    total=0;

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

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

創作回應

更多創作