【发布时间】:2020-10-04 00:59:58
【问题描述】:
我的程序有效。一旦我尝试用函数清理它,我就会完全迷失方向。有人可以检查我的代码并帮助我了解我做错了什么吗?
程序输出需要看起来像这样(请注意,这张照片是在添加功能之前拍摄的):
理想情况下,在update_level 循环之后,我会打印总数。
以下是我的代码,其中包含我完全不理解的功能。
#include <stdio.h>
int update_level(int player[],int healthpoint);
int display_levels();
int main(void)
{
int levels[6] = {1,2,3,4,5,6}; // levels is an array of 6 integers
int players[6] = {0,0,0,0,0,0}; // players is an array of 6 integers
int healthpoints; // initialize variable
printf("Enter total player health points (-1 to quit): "); // user input
scanf("%d", &healthpoints); // reads user input
for(j=0; j<=5; j++)
{
update_level(players[j],healthpoints);
}
return;
} // end main
int update_level(int player[],int healthpoint);
{
while (healthpoints != -1)
{
if ( healthpoints <= 9)
{
players[0] += 1;
} //end if
else if (healthpoints <= 19)
{
players[1] += 1;
} //end else if
else if (healthpoints <= 29)
{
players[2] += 1;
} //end else if
else if (healthpoints <= 39)
{
players[3] += 1;
} //end else if
else if (healthpoints <= 49)
{
players[4] += 1;
} //end else if
else
{
players[5] += 1;
} // end else
printf("Enter total player health points (-1 to quit): "); // user input
scanf("%d", &healthpoints); // reads user input
} // end while
return;
} // end update_level
int display_levels()
{
int i; // initialize variable
printf("T O T A L S\n\n");
for (i = 0; i < 6; ++i){
printf("Level %u%13d\n", levels[i], players[i]);
} // end for
return;
} // end display_levels
【问题讨论】:
-
你显示的代码有什么问题?
-
好吧,它不能编译。我不明白如何将数组传递给函数。
-
您是否尝试过使用
update_level(players, healthpoints)? -
如果它不能编译,编译器应该告诉你原因。你读了吗?错误信息是否有您不理解的地方?
-
是的,有很多我不明白的地方。您之前在不使用 for 循环的情况下尝试的评论帮助我找到了一些解决问题的方法。感谢您帮助我修复程序。