嗨,我知道范围内的随机发生器已经存在问题,但我不明白。我是C初学者,我只知道java。在这个程序中,我试图在 C 中创建一个数学导师。该程序将随机生成从 1 到 10 的两个数字和一个运算符。它运行,但不显示下一行,并且一直显示不正确的答案。另外,为什么 VS2010 说 getch() 是未定义的?这是代码:
int ans;
int ans1;
int num1 = rand() % 10 + 2;
int num2 = rand() % 10;
int operation = rand() % 4;
printf("\tMATH TUTOR\n");
if(operation == 1){
printf("What is %d + %d ?", num1, operation, num2);
scanf_s("%d",ans1);
ans = num1 + num2;
if(ans != ans1){
printf("Incorrect! Try Again!");
do{
scanf_s("%d", &ans1);
}while( ans != ans);
}else{
printf("Correct!");
}
}else if(operation == 2){
printf("What is %d - %d ?", num1, operation, num2);
scanf_s("%d",&ans1);
ans = num1 - num2;
if(ans != ans1){
printf("Incorrect! Try Again!");
do{
scanf_s("%d", &ans1);
}while( ans != ans);
}else{
printf("Correct!");
}
}else if(operation == 3 ){
printf("What is %d * %d ?", num1, operation, num2);
scanf_s("%d",&ans1);
ans = num1 * num2;
if(ans != ans1){
printf("Incorrect! Try Again!");
do{
scanf_s("%d", &ans1);
}while( ans != ans);
}else{
printf("Correct!");
}
}else if(operation == 4){
printf("What is %d / %d ?", num1, operation, num2);
scanf_s("%d",&ans1);
ans = num1 / num2;
if(ans != ans1){
printf("Incorrect! Try Again!");
do{
scanf_s("%d", &ans1);
}while( ans != ans);
}else{
printf("Correct!");
}
}
getch();
return 0;
}