【发布时间】:2016-11-10 04:02:21
【问题描述】:
所以我的程序中没有语法错误,这是一个逻辑错误。我的问题是,当我尝试运行它时,只会执行我的 printf 语句,但之后它会关闭我的程序,不会让我的 while loop 请求更多数据,直到用户输入 -1 来停止我的 while 循环。
#include <stdio.h>
// prototypes
void updateLevel(int PlayerPoints, int playerLevels[]);
void displayLevels(int ArrayName[]);
//main begins
int
main (void){
//arrays and varibles
int playerLevels[6] = {0};
int playerPoints = 0;
printf("Player points (-1 to quit) ");
scanf("%d" , &playerPoints);
//while loop to process input data
while(playerPoints =! -1){
scanf("Player points (-1 to quit) %d" , &playerPoints);
updateLevel(playerPoints, playerLevels);
}
displayLevels(playerLevels);
return(0);
}
//main ends
//functions
void updateLevel(int playerPoints, int playerLevels[]){
if(playerPoints >=50)
playerLevels[6]++;
else if (playerPoints >=40)
playerLevels[5]++;
else if (playerPoints >= 30)
playerLevels[4]++;
else if (playerPoints >= 20)
playerLevels[3]++;
else if (playerPoints >= 10)
playerLevels[2]++;
else
playerLevels[1]++;
}
void displayLevels(int playerLevels[]){
printf("T O T A L S\n");
printf("Level 1 %d\n", playerLevels[1]);
printf("Level 2 %d\n", playerLevels[2]);
printf("Level 3 %d\n", playerLevels[3]);
printf("Level 4 %d\n", playerLevels[4]);
printf("Level 5 %d\n", playerLevels[5]);
printf("Level 6 %d\n", playerLevels[6]);
}
【问题讨论】:
标签: c while-loop logic