| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 596 人关注过本帖
标题:这个猜数字游戏错哪里了?
只看楼主 加入收藏
逗比
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2014-10-31
结帖率:60%
收藏
 问题点数:0 回复次数:5 
这个猜数字游戏错哪里了?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
   
    srand((unsigned) time(0));
    int number = rand ()%100 + 1;
    int count = 0;
    int a = 0;
    printf("请猜数字");
    scanf("%d",&a);
    count++;
    while(count = 1,count++)
    {
     if (a = number){
        printf("Bingo!");
     }
     else if (0<=a<number){
             printf("Too small,请继续猜数字\n");
     }
     else if (a>number){
             printf("Too big,请继续猜数字\n");
        }
     else{
           printf("Game Over");
     }
    }
    while(1 < count <= 3, count++)
    {
     if (a = number){
        printf("Lucky You!");
     }
     else if(0<= a < number){
             printf("Too small,请继续猜数字\n");
     }
     else if(a >= number){
            printf("Too big,请继续猜数字\n");
     }
     else{
        printf("Game Over");
     }
    }
    while(count>3,count++)
    {
     if (a = number){
        printf("Good Guess!");
     }
     else if(0 <= a < number){
            printf("Too small,请继续猜数字\n");
     }
     else if(a > number){
            printf("Too big,请继续猜数字\n");
     }
     else{
        printf("Game Over");
     }
    }
    return 0;
 }
搜索更多相关主题的帖子: include number count 游戏 
2014-11-22 20:48
soulmate1023
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:256
专家分:831
注 册:2014-9-23
收藏
得分:0 
while全部都用错了,好好看看书喽
2014-11-22 20:58
魑魅魍魉!
Rank: 3Rank: 3
来 自:亲爱MM肚子里!
等 级:论坛游侠
威 望:1
帖 子:61
专家分:149
注 册:2014-5-3
收藏
得分:0 
看书
2014-11-22 21:45
Espierenergy
Rank: 2
等 级:论坛游民
帖 子:6
专家分:25
注 册:2014-11-29
收藏
得分:0 
/* 优化的程序,三次机会太少了!建议3<count,不然玩不起! */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
    srand((unsigned)time(0));
    int count = 3, a;
    int number = rand() % 100 + 1;
    while (count-- > 0)
    {
        printf("剩%d次 猜数字\n", count + 1);
        scanf("%d", &a);
        if (a == number)
        {
            printf("Bingo!");
        }
        else if (a < number)
        {
            printf("Too small,请继续猜数字\n");
        }
        else if (a > number)
        {
            printf("Too big,请继续猜数字\n");
        }
    }
    printf("Game Over :%d", number);
    return 0;
}

[ 本帖最后由 Espierenergy 于 2014-12-1 03:00 编辑 ]
2014-12-01 02:52
黄博森
Rank: 2
等 级:论坛游民
帖 子:34
专家分:28
注 册:2014-11-30
收藏
得分:0 
感觉你把你和计算机的角色弄反了,是你心里想一个数字,让计算机判断猜大了还是猜小了,选择再继续判断吗?
如果是的话,下面的例子可以参考:
#include<stdio.h>
#include<stdlib.h>
void main()
    {
      int low,high,mid,n;
      low = 1;
      high = 100;
      do
      {
        mid = (low+high)/2;
        system("cls");
        printf("是%d吗?\n(0猜中,1猜小了,2猜大了)",mid);
        scanf("%d",&n);
        switch(n)
        {
        case 1:
           low = mid+1;
           break;
        case 2:
           high = mid-1;
           break;
           }
        }while(n!=0&&low<=high);
        if(n==0)
        {
          printf("我猜中了!");
        }
        else
        {
          printf("范围超了\n");
        }
  }
是用二分法做的
2014-12-01 17:00
逆水寒刘
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:76
专家分:117
注 册:2013-3-27
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int Incount=0,num,Input;/*Icount用来记住猜的局数*/
void Start(void);
void Guess(void);
void Result(void);
void compare(void);
int main(void)
{   int con=1;/*用来控制循环进行,以便在用户不想玩时直接退出*/
    Start();
    while(con){
   Guess(Input);
   compare();
   if(Input==num)/*一旦用户猜对则直接退出循环*/
       break;
   printf("请选择是否继续:继续1,退出0:\n");
    scanf("%d",&con);
    }
   Result();
    return 0;
}
void Start()/*Start表示函数启动游戏;*/
{
    srand((unsigned)(time(0)));
    num=rand()%10+1;
   
 
}
void Guess()
{
    printf("请输入一个1-10的数:\n");
    scanf("%d",&Input);
 
}
void compare()
{
    if(Input>num)
        printf("很遗憾您猜的数大了!\n");
    else if(Input<num)
        printf("很遗憾您猜的数小了!\n");
    else
        printf("恭喜您猜对了!\n");
    Incount++;
}
void Result()
{

    printf("您总共猜了%d次,欢迎您再来!bye\n",Incount);
}
感觉楼主代码有点乱,可读性实在有些差。也是由于本人水平不高,所以自己又写了一个以供参考!建议楼主在代码旁写写注释!
2014-12-01 21:18
快速回复:这个猜数字游戏错哪里了?
数据加载中...
 
   



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

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