| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 674 人关注过本帖
标题:猜数字程序问题
只看楼主 加入收藏
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
结帖率:41.67%
收藏
 问题点数:0 回复次数:6 
猜数字程序问题
我给电脑输入一个答案, 然后让玩家才我输入的数字, 如果他猜高了我会提示他, 猜低了也提示, 但是如果猜超过8次的话, 程序就自动显出答案, 如果按0的话就退出。

我现在的程序其他的都没问题,除了玩家猜了8次后,程序还是不显答案, 还让玩家继续无限猜下去, 不知道问题出在哪里了, 大家帮忙看一下,谢谢啦

#include <iostream>
using namespace std;

int main ()
{
    const int num=56;
    int guess, i, flag=0, count=1;
   
    cout<<"Secret number between 1-100 generated..."<<endl;
   
    do
    {
        cout<<"Please enter your guess?";
        cin>>guess;
        
        if (guess<num && guess!=0)
        {
           if(num-guess<=5)
              cout<<guess<<" is lower than the answer but close..."<<endl;
           else
              cout<<guess<<" is lower than the answer..."<<endl;
        }
        
        else if (guess>num)
        {
           if(guess-num<=5)
              cout<<guess<<" is greater than the answer but close..."<<endl;
           else
              cout<<guess<<" is greater than the answer..."<<endl;
        }
        
        else if (guess==0 || count>8)
             flag=1;
        
        else
        {
            cout<<"Correct in "<<count<<" guesses."<<endl;
            flag=1;
        }
        count++;
    }while (flag != 1);
   
    if(count>8)
       cout<<"8 guesses. The secret number is "<<num<<"."<<endl;
    else if (guess==0)
       cout<<count<<" guesses. The secret number is "<<num<<"."<<endl;
   
    return 0;
}
搜索更多相关主题的帖子: 数字 
2010-04-08 11:56
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
if  与 else if不是这么用的



[ 本帖最后由 hahayezhe 于 2010-4-9 21:21 编辑 ]
2010-04-08 12:15
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
如果把else if全换成 if  最后的else 也改改就对了
2010-04-08 12:16
flyingzc
Rank: 2
等 级:论坛游民
帖 子:22
专家分:13
注 册:2010-4-1
收藏
得分:0 
#include <iostream>
using namespace std;

int main ()
{
    const int num=56;

    int guess, i, flag=0, count=1;
   
    cout<<"Secret number between 1-100 generated..."<<endl;
   
    do
    {
        cout<<"Please enter your guess?";
        cin>>guess;
        
        if (guess<num && guess!=0)
        {
           if(num-guess<=5)
              cout<<guess<<" is lower than the answer but close..."<<endl;
           else
              cout<<guess<<" is lower than the answer..."<<endl;
        }
        
        else if (guess>num)
        {
           if(guess-num<=5)
              cout<<guess<<" is greater than the answer but close..."<<endl;
           else
              cout<<guess<<" is greater than the answer..."<<endl;
        }
        
        else if (guess==0 || count>8)
             flag=1;
        
        else
        {
            cout<<"Correct in "<<count<<" guesses."<<endl;
            flag=1;
        }
        count++;
        if(count>8)
        {
            cout<<"8 guesses. The secret number is "<<num<<"."<<endl;
            break;
        }
        else if (guess==0)
            cout<<count<<" guesses. The secret number is "<<num<<"."<<endl;
        }while (flag != 1);
    return 0;
}这样就好啦
2010-04-08 12:40
kevin_cui
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-8-14
收藏
得分:0 
#include <iostream>
using namespace std;
int main ()
{
    const int num=56;
    int guess, flag=0, count=1;
   
    cout<<"Secret number between 1-100 generated..."<<endl;
   
    do
    {
        cout<<"Please enter your guess?";
        cin>>guess;
        if (guess==num)
        {
            cout<<"Congratulations! You guess the number"<<endl;
            flag=1;
        }
        if (guess<num)
        {
           if(num-guess<=5)
              cout<<guess<<" is lower than the answer but close..."<<endl;
           else
              cout<<guess<<" is lower than the answer..."<<endl;
        }
        
        if (guess>num)
        {
           if(guess-num<=5)
              cout<<guess<<" is greater than the answer but close..."<<endl;
           else
              cout<<guess<<" is greater than the answer..."<<endl;
        }
        if (guess==0)
        {
            cout<<count<<" guesses. The secret number is "<<num<<"."<<endl;
            flag=1;
        }
        count++;
        if (count>8)
        {
            cout<<"8 guesses. The secret number is "<<num<<"."<<endl;
            flag=1;
        }
        
    }while (flag != 1);
        
    return 0;
}
2010-04-08 17:33
cyhysr
Rank: 2
等 级:论坛游民
帖 子:8
专家分:17
注 册:2009-2-25
收藏
得分:0 
count应该放在循环里
2010-04-11 19:55
cyhysr
Rank: 2
等 级:论坛游民
帖 子:8
专家分:17
注 册:2009-2-25
收藏
得分:0 
count与8大小的判断应放在循环里
2010-04-11 19:56
快速回复:猜数字程序问题
数据加载中...
 
   



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

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