我正在处理一个类的任务(非分级),并且我不清楚为什么这段代码导致我的程序“挂起”与运行循环。虽然循环导致程序挂起
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nbStars = 0; // User defines the number of stars to display
int nbLines = 0; // User defines the number of lines on which to print
// Obtain the number of Stars to display
printf("Enter the number of Stars to display (1-3): ");
scanf("%d", &nbStars);
getchar();
// Limit the values entered to between 1 and 3
do {
printf("Enter the number of Stars to display (1-3): ");
scanf("%d", &nbStars);
if (nbStars < 1 || nbStars > 3) puts("\tENTRY ERROR: Please limit responses to between 1 and 3.\n");
} while (nbStars < 1 || nbStars > 3);
}
它在哪里挂?在getchar也许? – BlackBear
首先将\ n添加到显示的消息的末尾。另外:不要重复自己;您也可以将第一个scanf放入循环中(:=仅使用一个scanf()) – wildplasser
它以什么方式“挂起”?有什么症状? –