| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 344 人关注过本帖
标题:case转换错误在main函数中~~~拜托帮忙看下怎么回事啊
只看楼主 加入收藏
播种梦的小孩
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-3-19
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
case转换错误在main函数中~~~拜托帮忙看下怎么回事啊
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
main()
{
    int data1,data2,i;
    int count=0;
    int reply;

    srand(time(NULL));

    while( count < 10 )
    {
        i=rand()%4+1;

        switch(i)
        case 1:
        {
            data1=rand()%11;
            data2=rand()%11;

            printf("%d+%d=",data1,data2);
            scanf("%d",&reply);
        }
            break;

        case 2:
        {
            data1=rand()%11;
            data2=rand()%11;

            while (data1<data2)
            {
                data1=rand()%11;
                data2=rand()%11;
            }
            printf("%d-%d=",data1,data2);
            scanf("%d",&reply);
        }
            break;

        case 3:
        {
            data1=rand()%11;
            data2=rand()%11;

            printf("%d*%d=",data1,data2);
            scanf("%d",&reply);
        }
            break;

        case 4:
        {
            data1=rand()%11;
            data2=rand()%11;

            while(data2==0)
            {
                data1=rand()%11;
                data2=rand()%11;
            }

            while(data2!=0)
            {
                int x=fmod(data1,data2);
                while(x!=0)
                {
                    data1=rand()%11;
                    data2=rand()%11;
                }
            }

            printf("%d/%d=",data1,data2);
            scanf("%d",&reply);
        }
            break;

        count++;
    }
  }
搜索更多相关主题的帖子: include count 
2011-03-19 16:52
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:0 
修改以后的代码, 测试通过。
程序代码:
#include <time.h>
#include <stdio.h>

int main(void) {
    int data1,data2,i;
    int count=0;
    int reply;

    srand(time(NULL)); //个人感觉srand((unsigned)time(NULL))比较好。

    while( count < 10 )
    {
        i=rand()%4+1;

        switch(i) {          //switch语句要用大括号包好
        case 1:

            data1=rand()%11;
            data2=rand()%11;

            printf("%d+%d=",data1,data2);
            scanf("%d",&reply);

            break;

        case 2:

            data1=rand()%11;
            data2=rand()%11;

            while (data1<data2)
            {
                data1=rand()%11;
                data2=rand()%11;
            }
            printf("%d-%d=",data1,data2);
            scanf("%d",&reply);

            break;

        case 3:

            data1=rand()%11;
            data2=rand()%11;

            printf("%d*%d=",data1,data2);
            scanf("%d",&reply);

            break;

        case 4:

            data1=rand()%11;
            data2=rand()%11;

            while(data2==0)
            {
                data1=rand()%11;
                data2=rand()%11;
            }

            while(data2!=0)
            {
                int x=fmod(data1,data2);
                while(x!=0)
                {
                    data1=rand()%11;
                    data2=rand()%11;
                }
            }

            printf("%d/%d=",data1,data2);
            scanf("%d",&reply);

            break;
                }

        count++;
    }
                return 0;    
}


[ 本帖最后由 ansic 于 2011-3-19 17:05 编辑 ]

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-03-19 17:03
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:0 
测试一下, 会产生死循环呀

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-03-19 17:14
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:20 
修改完毕
程序代码:
root@~ #
root@~ #cat 1.c
#include <time.h>
#include <stdio.h>

int main(void) {
    int data1,data2,i;
    int count=0;
    int reply;

    srand((unsigned)time(NULL));

    while(count<10){
        i=rand()%4+1;
    
        switch(i) {
        case 1:

            data1=rand()%11;
            data2=rand()%11;

            printf("%d+%d=",data1,data2,data1+data2);
            scanf("%d",&reply);

            break;

        case 2:

            data1=rand()%11;
            data2=rand()%11;

            while (data1<data2)
            {
                data1=rand()%11;
                data2=rand()%11;
            }
            printf("%d-%d=",data1,data2,data1-data2);
            scanf("%d",&reply);

            break;

        case 3:

            data1=rand()%11;
            data2=rand()%11;

            printf("%d*%d=",data1,data2,data1*data2);
            scanf("%d",&reply);

            break;

        case 4:

            data1=rand()%11;
            data2=rand()%11+1; //data2永远不会是0啦

            printf("%d/%d=",data1,data2,data1/data2);
            scanf("%d",&reply);

            break;
                }

        count++;
    }
                return 0;
}
root@~ #

root@~ #./1
4-2=2
10/5=2
4*8=32
0/11=11
7/9=0
0*4=0
10/4=2
9*9=81
4-4=0
2*7=14
root@~ #

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-03-19 17:28
快速回复:case转换错误在main函数中~~~拜托帮忙看下怎么回事啊
数据加载中...
 
   



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

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