c语言由计算机生成10道100以内的2个数加法的式子,由用户进行解答,每道题有3次答题机会,第一次答对得10分, 第二次答对得9分,第三次答对得8分,三次都答不对得0分,最后输出每道题的得
时间: 2023-10-28 18:05:05 浏览: 18
分和总得分。
实现思路:
1. 使用rand()函数生成10个随机数,代表10道题目中的加数和被加数。
2. 将每个随机数限制在100以内,使用%运算符将其缩小到100以内。
3. 使用printf()函数输出每道题目,让用户输入答案,使用scanf()函数获取用户输入。
4. 使用if语句判断用户输入的答案是否正确,记录答题次数和得分。
5. 输出每道题目的得分和总得分。
代码如下:
相关问题
用C语言实现由计算机生成10道100以内的2个数加法的式子,由用户进行解答,每道题有3次答题机会,第一次答对得10分,第二次答对得9分,第三次答对得8分,三次都答不对得0分,最后输出每道题的得分情况和最后的总分。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int score = 0; // 总分
srand(time(NULL)); // 用系统时间初始化随机数种子
for (int i = 1; i <= 10; i++) { // 生成10道题目
int a = rand() % 100 + 1; // 生成1~100之间的随机数
int b = rand() % 100 + 1;
int ans = a + b; // 计算答案
int cnt = 0; // 答题次数
printf("%d. %d + %d = ", i, a, b);
while (cnt < 3) { // 最多答题3次
int userAns;
scanf("%d", &userAns);
cnt++;
if (userAns == ans) { // 答对了
switch (cnt) { // 根据答题次数计算得分
case 1:
score += 10;
printf("答对了!得10分。\n");
break;
case 2:
score += 9;
printf("答对了!得9分。\n");
break;
case 3:
score += 8;
printf("答对了!得8分。\n");
break;
}
break; // 跳出while循环
} else { // 答错了
if (cnt == 3) { // 答了3次都错了
printf("答错了。得0分。\n");
} else { // 还有机会
printf("答错了,请重新作答:");
}
}
}
}
printf("总分:%d分。\n", score);
return 0;
}
```
C语言设计一个程序,实现如下的功能:由计算机生成10道100以内的2个数加法的式子,由用户进行解答,每道题有3次答题机会,第一次答对得10分,第二次答对得9分,第三次答对得8分,三次都答不对得0分,最后输出每道题的得分情况和最后的总分。
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, j, answer, score, total_score = 0;
srand((unsigned)time(NULL)); // 初始化随机数种子
// 生成10道100以内的加法题
for (i = 1; i <= 10; i++) {
int a = rand() % 100 + 1;
int b = rand() % 100 + 1;
printf("第%d题:%d + %d = ", i, a, b);
// 用户最多答3次
for (j = 1; j <= 3; j++) {
scanf("%d", &answer);
if (answer == a + b) {
// 根据答题次数计算得分
if (j == 1) {
score = 10;
} else if (j == 2) {
score = 9;
} else {
score = 8;
}
printf("恭喜你,答对了!得%d分。\n", score);
total_score += score;
break;
} else {
printf("回答错误,请重新作答:");
}
}
// 三次都答错
if (j == 4) {
printf("很抱歉,正确答案是%d,本题得0分。\n", a + b);
}
}
printf("总得分为:%d分\n", total_score);
return 0;
}
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)