gpt4 book ai didi

c - 我试图识别在 do-while 中输入的学生人数,并在循环外显示该数字

转载 作者:行者123 更新时间:2023-11-30 21:05:00 24 4
gpt4 key购买 nike

我创建了一个接受用户输入的程序,并显示已输入的学生人数。

我使用 do-while 循环,因为在程序结束时会询问“你想继续吗?”如果用户输入 y,它将再次循环以获取输入。我尝试为其制作一个数组,但它无法很好地显示学生人数和平均成绩......

#include<stdio.h>


int main ()
{
int student_id, test1,test2, final_mark;
float total_up;
char answer;


do {

printf("\nProgram to Calculate Student Grade\n");
printf("Insert the student ID: ");
scanf("%d",&student_id);
printf("\nInsert marks for Test 1 (total of 25 ): ");
scanf(" %d",&test1);
if (test1 >25)
{
printf("The marks is greater than 25.Please re-insert it again.");
printf("\nInsert marks for Test 1 (total of 25 ): ");
scanf(" %d",&test1);
}
printf("\nInsert marks for Test 2 (total of 25 ): ");
scanf(" %d",&test2);
if (test2 >25)
{
printf("The marks is greater than 25.Please re-insert it again.");
printf("\nInsert marks for Test 2 (total of 25 ): ");
scanf(" %d",&test2);
}
printf("\nInsert marks for Final Exam: (total of 50): ");
scanf(" %d",&final_mark);
if ( final_mark > 50 )
{
printf("\nThe Final Exam is greater than 50. Please re-insert it again.");
printf("\nInsert marks for Final Exam: (total of 50) ");
scanf("%d",&final_mark);
}

total_up = test1 + test2 + final_mark;
printf("Total Marks: %.2f",total_up);
if (total_up >=80)
{
printf("\nGrade is A\n");
}
else if (total_up >=60)
{
printf("\nGrade is B\n");
}
else if (total_up >=50)
{
printf("\nGrade is C\n");
}
else if (total_up >=40)
{
printf("\nGrade is D\n");
}
else if (total_up <40)
{
printf("\nGrade is F\n");
}

printf("Do you want to continue? ");
scanf(" %c",&answer);
}while (answer == 'y' || answer == 'Y');

printf("\nTotal number of students entered is %d ", student_id);
total_up = total_up / 2;
printf("\nTotal number of students entered is %.2f ",total_up);
return 0;
}

假设输入为:

Program to Calculate Student Grade 
Insert the student ID : 123
Insert marks for Test 1 ( total of 25 ): 15
Insert marks for Test 2 ( total of 25 ): 20

Insert marks for Final Exam ( total of 50 ): 42

Total Marks : 77.00
Grade is B

Do you want to continue ? y

Program to Calculate Student Grade
Insert the student ID : 456
Insert marks for Test 1 ( total of 25 ): 21
Insert marks for Test 2 ( total of 25 ): 23

Insert marks for Final Exam ( total of 50 ): 47

Total Marks : 91.00
Grade is A

Do you want to continue ? n

所以,在 do-while 终止之后..我期望的是

Total number of students entered is 2
Total number of students entered is 84.00

Average grade is A

我为学生和测试尝试了一个数组,但它可能不起作用..有什么想法吗?

最佳答案

为了计算学生数量和计算小计,需要两个额外的变量并在循环外将它们初始化为 0。然后,在循环内部,随着循环的每次迭代增加student_count,并将当前学生的总分与计算小计的变量相加。最后,很容易计算平均值。只需将小计除以学生人数即可。

此外,为了计算成绩,您可以将其保留在另一个函数中,因为您必须执行两次,一次在循环内,另一次在循环外。

#include <stdio.h>
void showGrade(float marks)
{
if (marks >= 80) {
printf("Grade is A\n");
}
else if (marks >= 60) {
printf("Grade is B\n");
}
else if (marks >= 50) {
printf("Grade is C\n");
}
else if (marks >= 40) {
printf("Grade is D\n");
}
else if (marks < 40) {
printf("Grade is F\n");
}
}
int main()
{
int student_id, test1, test2, final_mark, student_count = 0;
float total_up, grand_total = 0, average;
char answer;
do {
student_count++;
printf("\nProgram to Calculate Student Grade\n");
printf("Insert the student ID: ");
scanf("%d", &student_id);
printf("\nInsert marks for Test 1 (total of 25 ): ");
scanf(" %d", &test1);
if (test1 > 25) {
printf("The marks is greater than 25. Please insert it again.");
printf("\nInsert marks for Test 1 (total of 25): ");
scanf(" %d", &test1);
}
printf("Insert marks for Test 2 (total of 25 ): ");
scanf(" %d",&test2);
if (test2 > 25) {
printf("The marks is greater than 25. Please insert it again.");
printf("\nInsert marks for Test 2 (total of 25): ");
scanf(" %d", &test2);
}
printf("Insert marks for Final Exam: (total of 50): ");
scanf(" %d", &final_mark);
if (final_mark > 50) {
printf("\nThe Final Exam is greater than 50. Please insert it again.");
printf("\nInsert marks for Final Exam: (total of 50): ");
scanf("%d", &final_mark);
}
total_up = test1 + test2 + final_mark;
grand_total += total_up;
printf("\nTotal Marks: %.2f\n", total_up);
showGrade(total_up);
printf("\nDo you want to continue? ");
scanf(" %c", &answer);
} while (answer == 'y' || answer == 'Y');
printf("\nTotal number of students entered is %d ", student_count);
average = grand_total / student_count;
printf("\nAverage total number of students entered is %.2f\n", average);
printf("\nAverage ");
showGrade(average);
return 0;
}

关于c - 我试图识别在 do-while 中输入的学生人数,并在循环外显示该数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com