我想让程序让用户输入正确的值,但是即使用户输入正确的值,while循环也会输出错误语句。我该如何解决这个问题?
int n, range, i;
printf("Enter an integer to find multiplication table :- \t");
while(i<0)
{
scanf("%d",&i);
if(i<0)
printf("Please enter a positive integer :- \n");
}
答案 0 :(得分:2)
初始化您的i值为-1,因为它初始化为null,因此您收到错误消息。 所以尝试运行此代码。
int n, range, i=-1;
printf("Enter an integer to find multiplication table: \t");
while(i<0)
{
scanf("%d", &i);
if (i<0)
printf("Please enter a positive integer: \n");
}
很抱歉,我犯了一个错误。
答案 1 :(得分:0)
试试这个
#include <stdio.h>
#include <string.h>
char* err[4]={"please", "!! take care, ","Dude! R u sober?", "C'mon! I say " };
int main()
{
int i, m=0, flg;
printf("Enter an integer to find multiplication table: \t");
scanf("%d", &i);
while(i<0)
{
if(m==4) {flg = 1; break;}
printf("\n%s enter a positive integer: \n", err[m]);
scanf("%d", &i);
m++;
}
if(!flg)printf("\n%d\n", i);
else printf("\nplease consider revising primary school's mathematics");
return 0;
}