| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 511 人关注过本帖
标题:c语言随机类型问题
只看楼主 加入收藏
我是含羞草
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2015-5-18
结帖率:60%
收藏
已结贴  问题点数:15 回复次数:5 
c语言随机类型问题
想要制作一个和电脑玩石头剪刀布的随机游戏。运行后:

Let’s play geme!

石头剪刀布…石头(0) 剪刀(1) 布(2)
The number is:4
Please select a number between 0 and 2
The number is:2

You : 布
I: 剪刀
You lose!

You wanna play one more time? Yes(1) or No(0)
The number is:1

石头剪刀布…石头(0) 剪刀(1) 布(2)
The number is:0

You :石头
I:剪刀
You win!

You wanna play one more time? Yes(1) or No(0)
The number is:1

石头剪刀布…石头(0) 剪刀(1) 布(2)
The number is:2

You:布
I:布
Even!

You wanna play one more time? Yes(1) or No(0)
The number is:3
Input 0 or 1.
The number is:0
Good bye



老师什么都没说就让我们做了,好像要用到下列代码,但我不知道放哪里和怎么用:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void){
 int a;
 srand(time(NULL));
 a=rand()%5;
}

麻烦老手解答!谢谢!
搜索更多相关主题的帖子: 石头剪刀布 between number c语言 
2015-05-27 22:34
我是含羞草
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2015-5-18
收藏
得分:0 
有人知道怎么做的吗,我今天试了下,不会啊 。求教!
2015-05-28 11:29
TAAAAB
Rank: 7Rank: 7Rank: 7
来 自:湖南
等 级:黑侠
威 望:1
帖 子:243
专家分:635
注 册:2011-5-29
收藏
得分:5 
个人理解如下,供参考
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void){
int a;
srand(time(NULL));//利用系统时间来改变的种子值,放在要写的代码前面,出现一次
a=rand()%5;    //这里改为a=rand()%3;因为应该只要随机生成三种状态0 1 2。
               //放在srand()后面的任何地方,需要随机数的时个用a=rand()%3来生成随机数a

.
.
.

}


[ 本帖最后由 TAAAAB 于 2015-5-28 11:44 编辑 ]

人有多懒,编程就有多难。
2015-05-28 11:42
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:5 
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int f(int a,int b){
    if(a==b)return 1;
    if((a+1)%3==b)return 2;
    return 3;
}
int main(void){
    int a,b,ch=1;
    char *res[3]={"石头","剪刀","布"};
    srand(time(NULL));
    a=rand()%3;
    printf("Let’s play geme!\n\n");
    do{
        printf("石头剪刀布…石头(0) 剪刀(1) 布(2)\n");
        if(ch==0)break;
        if(ch==1){
            printf("The number is:");
            scanf("%d",&b);
            while(b>2||b<0){
                printf("Please select a number between 0 and 2\n");
                printf("The number is:");
                scanf("%d",&b);
            }
            if(b<=2&&b>=0){
                printf("\n\nYou :%s\n",res[b]);
                printf("I:%s\n",res[a]);
            }
            switch(f(a,b)){
                case 1:printf("Even!\n\n");break;
                case 2:printf("You win!\n\n");break;
                case 3:printf("You lose!\n\n");break;
            }   
        }
        printf("You wanna play one more time? Yes(1) or No(0)\n");
        printf("The number is:");
        scanf("%d",&ch);
        while(ch!=0&&ch!=1){
            printf("Input 0 or 1.\n");
            printf("The number is:");
            scanf("%d",&ch);
        }
        if(ch==0)break;
    }while(1);
    printf("Good bye");
}

剑栈风樯各苦辛,别时冰雪到时春
2015-05-28 13:06
我是含羞草
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2015-5-18
收藏
得分:0 
4楼的好象运行不了额= =
2015-05-28 18:00
纳兰伽香
Rank: 10Rank: 10Rank: 10
来 自:北京
等 级:贵宾
威 望:10
帖 子:426
专家分:1650
注 册:2015-4-5
收藏
得分:5 
这个很好写啊  用 0 1 2 表示石头、剪刀、布   然后用time函数和rand函数随机生成 0 1 2 这3个数。
接下来的工作就是比较了。思路给你 自己写吧。

风回小院庭芜绿,柳眼春相续
2015-05-28 18:05
快速回复:c语言随机类型问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018506 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved