| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1273 人关注过本帖
标题:请大家看看这程序错在哪里了?
只看楼主 加入收藏
hopepark
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2008-3-23
收藏
得分:0 
如果要加入显示电脑出了什么和你自己出了什么该怎么修改呢?如果再加上你现在几胜几败又该如何修改呢?
2008-09-13 11:04
kscus
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-9-13
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
        int Player,Computer,ping,win,lose;
        int single=0;
            ping=0;
            win=0;
            lose=0;
        goto start1;
  start: printf("please choose once again or end,once again input 1,end input 2.\n");
         printf("yes or no:%i",single);
         scanf("%i",&single);
         if(single==1)
            goto start1;
            else if(single==2)
                  goto end;
                  else
                     printf("\nError choose,please choose again!\n");
                         goto start;
  start1:  printf("Start the game already\n" );

  begin:    printf ( "Plese input a number\n 1Scissors\n 2Rock\n 3Paper\n 4Quit\n");
            scanf ( "%d", &Player);
            if (Player>=4||Player<=0)
            {
                printf ("Inputerror!\nPlease input again!\n " );
                goto begin;
            }


            srand (time(NULL));
            Computer=1+rand()%3;
            printf("%d\n",Computer);


            if(Computer==Player)
               printf ("Ping Ju!\n",ping++);
                else if((Player-Computer)==1)
                    printf ("YOU WIN!\n",win++);
                    else if
                        (Player==1&&Computer==3)
                        printf("YOU WIN!\n",win++);
                        else
                           printf("YOU LOSE!\n",lose++);
                       single=0;
                       goto start;
                            

  end:   printf("the total number of the game is :%i\n",ping+win+lose);
         printf("out of the game and good luck!\n");
         printf("this is your grade:\n");
         printf("ping: %i\nwin: %i\nlose: %i\n",ping,win,lose);
}

这是我改写的,能够统计游戏次数,也能够统计总次数,而且可以无限制的游戏。不过我改来改去始终不能用 y 来表示继续,用 n 来表示结束,我尝试着用字符变量来表示,可是不知道怎么回事,那个  printf("\nError choose,please choose again!\n");始终会被调用两次,代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
        int Player,Computer,ping,win,lose;
        char single='\0';
            ping=0;
            win=0;
            lose=0;
        goto start1;
  start: printf("please choose once again or end,once again input y,end input n.\n");
         printf("yes or no:%c",single);
         scanf("%c",&single);
         if(single=='y')
            goto start1;
            else if(single=='n')
                  goto end;
                  else if(single='\0')
                     goto start;
                     else
                       printf("\nError choose,please choose again!\n");
  clear:                 single='\0';
                         goto start;
  start1:  printf("Start the game already\n" );

  begin:    printf ( "Plese input a number\n 1Scissors\n 2Rock\n 3Paper\n 4Quit\n");
            scanf ( "%d", &Player);
            if (Player>=4||Player<=0)
            {
                printf ("Inputerror!\nPlease input again!\n " );
                goto begin;
            }


            srand (time(NULL));
            Computer=1+rand()%3;
            printf("%d\n",Computer);


            if(Computer==Player)
               printf ("Ping Ju!\n",ping++);
                else if((Player-Computer)==1)
                    printf ("YOU WIN!\n",win++);
                    else if
                        (Player==1&&Computer==3)
                        printf("YOU WIN!\n",win++);
                        else
                           printf("YOU LOSE!\n",lose++);
                 goto clear;
                            

  end:   printf("the total number of the game is :%i\n",ping+win+lose);
         printf("out of the game and good luck!\n");
         printf("this is your grade:\n");
         printf("ping: %i\nwin: %i\nlose: %i\n",ping,win,lose);
}
我也是个新手,程序写得很臭(已被前辈骂过数次了),请各位大侠不吝赐教啦!o(∩_∩)o...

[[it] 本帖最后由 kscus 于 2008-9-14 13:18 编辑 [/it]]
2008-09-13 14:58
kscus
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-9-13
收藏
得分:0 
[bo][un]hopepark[/un] 在 2008-9-13 11:04 的发言:[/bo]

如果要加入显示电脑出了什么和你自己出了什么该怎么修改呢?如果再加上你现在几胜几败又该如何修改呢?

那个其实你的程序里面已经有了,可能你没注意,就是 printf("%d\n",Computer)
如果想明确一点的话,可以写为 printf("Computer's key is:%d\n",Computer),然后在前面 scanf ( "%d", &Player);语句之后加上想要的printf 语句就行了。

[[it] 本帖最后由 kscus 于 2008-9-13 15:33 编辑 [/it]]
2008-09-13 15:08
hopepark
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2008-3-23
收藏
得分:0 
[bo][un]kscus[/un] 在 2008-9-13 15:08 的发言:[/bo]


那个其实你的程序里面已经有了,可能你没注意,就是 printf("%d\n",Computer)
如果想明确一点的话,可以写为 printf("Computer's key is:%d\n",Computer),然后在前面 scanf ( "%d", &Player);语句之后加上想要的p ...

我确实这样写了,但是我的意思是电脑这样写出来Computer's key is:石头 或者剪刀什么的,而我写的这个也仅仅是1,2,3,
2008-09-14 11:05
hopepark
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2008-3-23
收藏
得分:0 
[bo][un]kscus[/un] 在 2008-9-13 14:58 的发言:[/bo]

#include
#include
#include

int main(void)
{
        int Player,Computer,ping,win,lose;
        int single=0;
            ping=0;
            win=0;
            lose=0;
        goto start1; ...

谢谢指教,我还是菜鸟蛋
2008-09-14 11:11
kscus
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-9-13
收藏
得分:0 
[bo][un]hopepark[/un] 在 2008-9-14 11:05 的发言:[/bo]


我确实这样写了,但是我的意思是电脑这样写出来Computer's key is:石头 或者剪刀什么的,而我写的这个也仅仅是1,2,3,

这个也很简单,加几句if else   printf;语句就可以了,代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
        int Player,Computer,ping,win,lose;
        int single=0;
            ping=0;
            win=0;
            lose=0;
        goto start1;
  start: printf("please choose once again or end,once again input 1,end input 2.\n");
         printf("yes or no:%i",single);
         scanf("%i",&single);
         if(single==1)
            goto start1;
            else if(single==2)
                  goto end;
                  else
                     printf("\nError choose,please choose again!\n");
                         goto start;
  start1:  printf("Start the game already\n" );

  begin:    printf ( "Plese input a number\n 1Scissors\n 2Rock\n 3Paper\n");
            scanf ( "%d", &Player);
            if(Player==1)
            printf("player's key is: 剪刀\n");
            else if(Player==2)
            printf("player's key is:  石头\n");
            else if(Player==3)
            printf("player's key is:  布\n");
            
            if (Player>=4||Player<=0)
            {
                printf ("Inputerror!\nPlease input again!\n " );
                goto begin;
            }


            srand (time(NULL));
            Computer=1+rand()%3;
            if(Computer==1)
            printf("computer's key is: 剪刀\n");
            else if(Computer==2)
            printf("computer's key is:  石头\n");
            else if(Computer==3)
            printf("computer's key is:  布\n");


            if(Computer==Player)
               printf ("Ping Ju!\n",ping++);
                else if((Player-Computer)==1)
                    printf ("YOU WIN!\n",win++);
                    else if
                        (Player==1&&Computer==3)
                        printf("YOU WIN!\n",win++);
                        else
                           printf("YOU LOSE!\n",lose++);
                       single=0;
                       goto start;
                            

  end:   printf("the total number of the game is :%i\n",ping+win+lose);
         printf("out of the game and good luck!\n");
         printf("this is your grade:\n");
         printf("ping: %i\nwin: %i\nlose: %i\n",ping,win,lose);
}

不过我还没怎么接触过随机函数,所以那个srand (time(NULL));
                                      Computer=1+rand()%3;  是什么意思我还没看明白,嘿嘿,麻烦楼主给兄弟我解释一下!另外我觉得这个随机数的产生还不是很灵活,我反复做了几次试验发现电脑产生的随机数重复性很大,不知道楼主有没有什么好的解决办法,我在这里受教了……
2008-09-14 13:16
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
LS:少用goto。

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-09-14 13:56
kscus
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-9-13
收藏
得分:0 
[bo][un]StarWing83[/un] 在 2008-9-14 13:56 的发言:[/bo]

LS:少用goto。

少用?呵呵,我只是觉得这个用起来挺方便的,就是不知道用多了有什么后果……
2008-09-14 17:01
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
有一种代码,叫做“意大利面条式的代码”,满代码都是goto,导致代码像面条一样缠绕,极端难以维护,你是没体会过的,反正某些人代码出错了,直接往论坛一丢就走人,丝毫也不愿意自己Debug。

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-09-14 19:15
hopepark
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2008-3-23
收藏
得分:0 
[bo][un]kscus[/un] 在 2008-9-14 13:16 的发言:[/bo]


这个也很简单,加几句if else   printf;语句就可以了,代码如下:
#include
#include
#include

int main(void)
{
        int Player,Computer,ping,win,lose;
        int single=0;
            ping= ...

srand(time(null))是利用时间设置随机种子,只有一秒以后你运行一次这程序,生成的随机数就会不一样,如果不用这个很有可能一直生成的随机数都是同一个;Computer=1+rand()%3;是为了让生成的随机数+1.如果Computer= rand()%3 就是生成0,1,2这三个数,为了让选择时候1,2,3这样选择,所以加了一个1.这样Computer=1+rand()%3就可以生成1,2,3这三个数
2008-09-15 10:37
快速回复:请大家看看这程序错在哪里了?
数据加载中...
 
   



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

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