嗨,我知道范围内的随机发生器已经有一个问题,但我不明白。我是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;
}
我改变了它,但随机发生器不能正常工作。这是如何把一个范围放在随机发生器上? int operation = rand()%4 + 1 – eLg
是的,这将工作。您的代码中还有其他问题。看看你的printf的。你的代码是printf(“什么是%d +%d?”,num1,operation,num2);你有%d +(运营商)%d。但是你指定了三个参数... num1,operator,num2。你应该从这个列表中删除操作符。 –
谢谢..我有一个问题,虽然每当我运行该程序时,它一直显示2 * 8,并且当我输入正确的答案时,它一直显示错误再试一次 – eLg