gpt4 book ai didi

c - 如何使用 while 循环

转载 作者:行者123 更新时间:2023-11-30 14:21:13 25 4
gpt4 key购买 nike

我是编程新手,最近发现这个网站可以帮助我进行 self 教育。我正在尝试使用“C”制作一个程序,提示我书中三个不同类别的成绩,我遇到了 while 语句,并认为它们是我执行此操作的最佳选择。我当前的代码看起来像这样。

int countA;
int gradeA;
int totalA;
int weightA;
float averageA;

int countE;
int gradeE;
int totalE;
int weightE;
float averageE;

int countQ;
int gradeQ;
int totalQ;
int weightQ;
float averageQ;

totalA = 0;
countA = 0;

totalE = 0;
countE = 0;

totalQ = 0;
countQ = 0;

printf( "Enter Assignment Grade, -1 to end: "); /* prompt for input */
scanf( "%d", &gradeA );

while (gradeA != -1){
totalA = totalA + gradeA; /* add gradeA to totalA */
countA = countA + 1;
printf( "Enter Assignment Grade, -1 to end: "); /* prompt for input */
scanf( "%d", &gradeA );
}
if (countA != 0) {
averageA = (float) totalA / countA;
printf( "total is %.2f\n", averageA );
}



printf( "Enter Exam Grade, -1 to end: "); /* prompt for input */
scanf( "%d", &gradeE );

while (gradeE != -1){
totalE = totalE + gradeE; /* add gradeE to totalE */
countE = countE + 1;
printf( "Enter Exam Grade, -1 to end: "); /* prompt for input */
scanf( "%d", &gradeE );
}
if (countE != 0) {
averageE = (float) totalE / countE;
printf( "total is %.2f\n", averageE );
}


printf( "Enter Quiz Grade, -1 to end: "); /* prompt for input */
scanf( "%d", &gradeQ );

while (gradeQ != -1){
totalQ = totalQ + gradeQ; /* add gradeQ to totalQ */
countQ = countQ + 1;
printf( "Enter Quiz Grade, -1 to end: "); /* prompt for input */
scanf( "%d", &gradeQ );
}
if (countQ != 0) {
averageQ = (float) totalQ / countQ;
printf( "total is %.2f\n", averageQ );
}

现在,我在这一点之后尝试做的是再重复该过程两次,但是当我尝试运行 exe 时,我得到了第一个部分来运行,但其他两个部分根本没有被提出来原因。这是否只是对 while 重复语句的作用进行限制?或者我在某个地方有错误。我试图找出我做错了什么,但我只是看到了它。

我还不太确定如何在此处正确发布,但这是我得到的输出的示例。

这是我的输出示例,如您所见,它提示我输入第一段,即作业部分,但在输入 -1 结束循环后,它只给出平均值并结束。

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin>project1.exe
Enter Assignment Grade, -1 to end: 100
Enter Assignment Grade, -1 to end: 80
Enter Assignment Grade, -1 to end: 77
Enter Assignment Grade, -1 to end: 33
Enter Assignment Grade, -1 to end: 76
Enter Assignment Grade, -1 to end: 92
Enter Assignment Grade, -1 to end: -1
total is 76.33

我认为至少第一部分是有效的,但在我得到总分后,我不会提示我进行下一步查看,然后询问考试成绩。

最佳答案

您可能遇到的一个常见问题是输出缓冲。对于您的代码:

printf( "Enter Assignment Grade, -1 to end: "); /* prompt for input */
scanf( "%d", &gradeA );

您的终端可能不会将输出缓冲区刷新到屏幕。所以它会等待输入,但你可能看不到提示。要强制执行此操作,您可以执行以下操作:

printf( "Enter Assignment Grade, -1 to end: "); /* prompt for input */
fflush(stdout);
scanf( "%d", &gradeA );

现在,这会导致更多的重复。如果您只需为每个循环编写一次提示而不是两次,那就更好了。您可以通过在循环之前将 gradeA 初始化为零来解决这个问题。那么循环内的第一个计算不会产生任何效果:

gradeA = 0;
totalA = -1;

while (gradeA != -1)
{
totalA = totalA + gradeA;
countA = countA + 1;
printf( "Enter Assignment Grade, -1 to end: ");
fflush(stdout);
scanf( "%d", &gradeA );
}

请注意,我将 countE 设置为 -1,以撤消在循环顶部递增它的事实。这一切开始让人感觉有点笨拙。

另一个问题是,如果用户输入的内容不是整数,您的程序将产生未定义的行为(因为您实际上并未初始化gradeA)。您可以测试 scanf 是否成功,因为它返回读取的项目数。

懒惰的方法是这样的:

if( 1 != scanf( "%d", &gradeA ) ) {
printf( "Invalid input!"\n" );
exit(1);
}

你可以决定。对于其余的答案,我会忽略这一点。

当您加强循环时,很明显代码重复是一个主题。您所更改的只是变量和提示。所以可能是时候将它变成一个函数了:

int grade_average( const char *what, int *total, float *average )
{
int count = -1;
int grade = 0;
*total = 0;

while( grade != -1 )
{
count++;
*total += grade;

printf( "Enter %s Grade, -1 to end: ", what );
fflush(stdout);

if( 1 != scanf("%d", &grade) ) return 0; // fail on input error.
}

if( count > 0 ) {
*average = (float)*total / count;
}

return 1;
}

现在你可以这样调用:

if( grade_average("Assignment", &totalA, &averageA) ) {
printf( "Total is %.2f\n", averageA );
}

if( grade_average("Exam", &totalE, &averageE) ) {
printf( "Total is %.2f\n", averageE );
}

if( grade_average("Quiz", &totalQ, &averageQ) ) {
printf( "Total is %.2f\n", averageQ );
}

关于c - 如何使用 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14864239/

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