用C语言写生成10个随机一百以内的加减乘除的算式,之后用户输入十个算式的答案,判断对错,之后刷新再来,如此循环的代码
时间: 2023-04-03 11:04:18 浏览: 103
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i, answer, user_answer, count = 0;
char operator;
srand(time(NULL)); // 设置随机数种子
while (1) {
printf("第%d题:", count + 1);
int num1 = rand() % 100 + 1; // 生成1-100之间的随机数
int num2 = rand() % 100 + 1;
int op = rand() % 4; // 生成0-3之间的随机数,分别代表加减乘除
switch (op) {
case 0:
operator = '+';
answer = num1 + num2;
break;
case 1:
operator = '-';
answer = num1 - num2;
break;
case 2:
operator = '*';
answer = num1 * num2;
break;
case 3:
operator = '/';
answer = num1 / num2;
break;
}
printf("%d %c %d = ", num1, operator, num2);
scanf("%d", &user_answer);
if (user_answer == answer) {
printf("回答正确!\n");
} else {
printf("回答错误!正确答案为%d\n", answer);
}
count++;
if (count == 10) {
printf("本轮结束,是否继续?(Y/N)");
char choice;
scanf(" %c", &choice);
if (choice == 'N' || choice == 'n') {
break;
} else {
count = 0;
}
}
}
return 0;
}
阅读全文