| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1188 人关注过本帖
标题:有个问题,自己琢磨一天了,实在想不出,还得麻烦论坛里的朋友啊!!!
取消只看楼主 加入收藏
经典记忆
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2012-7-19
结帖率:16.67%
收藏
已结贴  问题点数:10 回复次数:8 
有个问题,自己琢磨一天了,实在想不出,还得麻烦论坛里的朋友啊!!!
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    char another_game = 'Y';
    bool correct = false;
    int counter = 0;
    int sequence_length = 0;
    int i = 0;
    time_t seed = 0;
    int number = 0;
    time_t now = 0;
    time_t time_taken = 0;
    printf("\nTo play Simple Simon,");
    printf("watch the screen for a sequence of digits.");
    printf("\nWatch carefully,as the digits are only displayed"
    " for a second! ");
    printf("\nThe computer will remove them, and then prompt you");
    printf("to enter the same sequence.");
    printf("\When you do, you must put spaces between the digits.\n");
    printf("\nGood Luck!\nPress Enter to play\n");
    scanf("%c",&another_game);
    do
    {
                              correct = true;
                              counter = 0;
                              sequence_length = 2;
                              time_taken = clock();
                              while(correct)
                              {
                                            sequence_length += counter++%3 == 0;
                                            seed = time(NULL);
                                            now = clock();
                                            srand((unsigned int)seed);
                                            for(i = 1;i<=sequence_length;i++)
                                             printf("%d",rand()%10);
                                             for(;clock()-now<CLOCKS_PER_SEC;);
                                             printf("\r");
                                             for(i =1;i<=sequence_length;i++)
                                              printf("  ");
                                              if(counter==1)
                                               printf("\nNow you enter the sequence_don't forget"
                                               " the spaces\n");
                                              else
                                               printf("\r");
                                               
                                               srand((unsigned int)seed);
                                               for(i = 1;i<=sequence_length;i++)
                                               {
                                                     scanf("%d",&number);
                                                     if(number !=rand()%10)
                                                     {
                                                               correct = false;
                                                               break;
                                                     }
                                            
                                               }
                                               printf("%s\n",correct?"correct!":"wrong!");
                              }
                              time_taken = (clock()-time_taken)/ CLOCKS_PER_SEC;
                              printf("\n\nYour score is %d",--counter * 100/time_taken);
                              fflush(stdin);
                              printf("\nDo you want to play again(y/n);");
                              scanf("%c",&another_game);
    }
    while(toupper(another_game)=='Y');
    getchar();
    getchar();
    return 0;
}
   
执行后,我正确的输入结果也是“wrong”,我怀疑下面的语句有问题:
for(i = 1;i<=sequence_length;i++)
 {
         scanf("%d",&number);
         if(number !=rand()%10)
         {
              correct = false;
              break;
         }
 }
我认为scanf读取的是所有的数字,而if(number !=rand()%10)的意思是是对每个数字进行判断,所以问题应该出在这里,而我又不知道怎么改,恳请哪位老师帮忙看一下,我万分感激!!,语句很长,您幸苦了!!!!!
搜索更多相关主题的帖子: carefully correct 朋友 sequence 
2012-08-05 16:48
经典记忆
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2012-7-19
收藏
得分:0 
一个小游戏,就是系统随机显示几个数,停留一秒后消失,你凭记忆去输入,正确输入三次后,数字会增加,直到错了位止,我的问题是无论我输入什么我都是错的,所以我怀疑上述位置scanf有问题
2012-08-05 16:55
经典记忆
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2012-7-19
收藏
得分:0 
老师,是不是我所说的scanf的问题?
2012-08-05 17:09
经典记忆
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2012-7-19
收藏
得分:0 
对,我说的就是这个意思,我想每个数字去判断,可是不知道怎么弄,我是新手,这个是书上的一个例子,书上也是这么写的。可是我执行的话就有问题
2012-08-05 17:15
经典记忆
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2012-7-19
收藏
得分:0 
用char来读取是这样改吗:
char number = 0;
scanf("%c",&number);

老师,幸苦一下,把你改过的让我看看吧!!
谢谢!!
2012-08-05 17:32
经典记忆
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2012-7-19
收藏
得分:0 
怎么改啊 我还不明白啊  呵呵
2012-08-05 17:34
经典记忆
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2012-7-19
收藏
得分:0 
scanf("%1d",&number);和scanf("%d",&number);有什么区别吗?
2012-08-05 17:37
经典记忆
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2012-7-19
收藏
得分:0 
但是据我目前的学习,%ld只是在读取long类型的数据它和%d可以相互转化的啊,再有,我按照ld改后,还是不行啊,
2012-08-05 17:45
经典记忆
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2012-7-19
收藏
得分:0 
好了,我知道了,我把1当成L(小写)了,多谢各位
2012-08-05 17:50
快速回复:有个问题,自己琢磨一天了,实在想不出,还得麻烦论坛里的朋友啊!!! ...
数据加载中...
 
   



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

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