我在调用srand的语法时遇到问题,为添加问题插入随机数?尝试查找类似的程序来查看语法而没有运气。
#include<stdio.h>
#include"simpleio.h"
int main()
{
// int seed;scanf("%d",&seed);
// srand((unsigned)seed);
int decision,n,o ;
// scanf("%d", &n);
// srand((unsigned)n);
do{
printf("1.Give me an addition problem.\n"
"2.Give me a subtraction problem.\n"
"3.Give me a multiplication problem.\n"
"4.Quit\n");
scanf("%d", & decision);
} while (decision>5 && decision<0);
if (decision==1)
{
printf("1+1= ");
}
else if (decision==2)
{
printf("1-2\n");
}
else if (decision==3)
{
printf("1*2\n");
}
else if(decision==4)
{
printf("\n");
}
return 0;
}
答案 0 :(得分:0)
在类似的任务中,最好使用switch case
:
#include <stdio.h>
#include <string.h>
main()
{
int decision;
do{
printf("1.Give me an addition problem.\n"
"2.Give me a subtraction problem.\n"
"3.Give me a multiplication problem.\n"
"4.Give me a division problem.\n"
"5.Quit\n");
scanf("%d", & decision);
} while (decision >5 || decision <= 0); // correct your condition
// generate two random numbers for the operation
srand(time(NULL));
int a = rand()%20 + 1; // random between 1 and 20
int b = rand()%20 + 1; // random between 1 and 20
switch(decision)
{
case 1: //addition
printf("%d + %d = %d\n", a, b, a+b);
break;
case 2: //subtraction
printf("%d - %d = %d\n", a, b, a-b);
break;
case 3: //multiplication
printf("%d * %d = %d\n", a, b, a*b);
break;
case 4: //division
printf("%d / %d = %d, and remainder = %d\n", a, b, a/b, a%b);
default :
break;
}
return;
}
我将该部门列为奖金