我正在尝试制作一个程序,在数组中打印10-50之间的10个整数,当您输入10-50以外的整数时,它会询问您是否要重试。但当我试图进入";n〃;它仍然会继续询问另一个整数。我做错了什么?
#include <stdio.h>
#include <stdlib.h>
#define size 10
int main() {
int i,arr[size],sum=0;
char ch;
do {
for(i = 0; i < size; i++){
printf("Input a number at index %d: ", i);
scanf("%d", &arr[i]);
if(arr[i] <10 ||arr[i] > 50 ){
printf("entered number is not validn");
printf ("Do you want to repeat the operation Y/N: ");
scanf (" %c", &ch);
}
}
printf ("Do you want to repeat the operation Y/N: ");
scanf (" %c", &ch);
}
while (ch == 'y' || ch == 'Y');
}
在这个if
语句中,您询问用户是否要继续,但不处理用户输入"n"的情况。必须先输入比较,然后输入break
才能退出循环。
if (arr[i] < 10 || arr[i] > 50){
printf("entered number is not in the range 10..50n");
printf("Do you want to repeat the operation Y/N: ");
scanf(" %c", &ch);
// ADD THE FOLLOWING TO YOUR CODE
if (ch != 'y' && ch != 'Y')
break;
}