void add1() { Time=time(NULL); drawTheGameBox(); int num_rand_1,num_rand_2; int n,y=3,x=26; srand(time(NULL)); for(int i=0;i<18;i++) { gotoxy(25,y+=1); num_rand_1=rand()%11; num_rand_2=rand()%11; printf("%2d+%2d=",num_rand_1,num_rand_2); scanf("%d",&n); if(n==num_rand_1+num_rand_2) { gotoxy(65,y); n++; sorce+=10; printf("答对啦!"); gotoxy(x+=1,22); printf("★"); drawTheGameBox(); }else{ gotoxy(65,y); printf("答错啦,继续加油!"); drawTheGameBox(); } }如何将上述代码中num_rand_1+num_rand_2的’+’改为从‘+’,‘-’,‘*’,‘/’这四个字符中随机抽取一个输出
时间: 2024-03-18 08:40:09 浏览: 54
add1顶部菜单.rar_css头部菜单_js 顶部菜单_jsmenu.add_菜单_菜单页面
你可以使用rand()函数生成一个0到3的随机整数,然后根据不同的数值输出不同的运算符。例如:
```
int operator_index = rand() % 4;
char operator_char;
switch(operator_index) {
case 0:
operator_char = '+';
break;
case 1:
operator_char = '-';
break;
case 2:
operator_char = '*';
break;
case 3:
operator_char = '/';
break;
}
printf("%2d %c %2d = ", num_rand_1, operator_char, num_rand_2);
```
这样就可以实现将运算符随机抽取一个输出。
阅读全文