【发布时间】:2015-04-29 09:14:23
【问题描述】:
我想编写一个简单的程序,其中rand() 函数从 1、2、3 中生成一个随机数,并要求用户预测该数字。如果用户正确地预测了数字,那么他就赢了,否则他就输了。
这是程序-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int game;
int i;
int x;
printf("enter the expected value(0,1,2)");
scanf("%d\n",&x);
for(i=0;i<1;i++){
game=(rand()%2) + 1
if(x==game){
printf("you win!");
}
else{
printf("you loose!");
}
} return 0;
}
【问题讨论】:
-
你的问题是什么?
-
我认为
game=(rand()%2) + 1必须是game=(rand()%3)因为你在第一个printf中使用了0,1,2 -
@CoolGuy 对,将此添加到我的答案中。 :-)
-
为什么需要循环?