m0_56526691 2021-08-02 11:28 采纳率: 100%
浏览 328
已结题

c++中怎么限制while的次数,使下面这个进行第五次循环时判定失败

#include
#include
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
int num = rand() % 100 + 1;
int a = 0;
cout << "请输入您所认为的数字" << endl;
cin >> a;
int c = 0;
while (c <= 5)
{
c = c + 1;
if(c<=5){
if (a > num)
{
cout << "错误,数字过大" << endl;

        }
        else if (a < num)
        {

            cout << "错误,数字偏小" << endl;

        }
        else
        {
            cout << "正确" << endl;
            break;
        }




    }

}
system("pause");
return 0;

}

  • 写回答

5条回答 默认 最新

  • qzjhjxj 2021-08-02 11:53
    关注

    修改如下,供参考:

    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    int main()
    {
        srand((unsigned int)time(NULL));
        int num = rand() % 100 + 1;
        int a = 0;
        cout << "请输入您所认为的数字" << endl;
        cin >> a;
        int c = 0;
        while (c != 5-1)
        {
            //if (c <= 5) {
            if (a > num)
            {
                cout << "错误,数字过大" << endl;
            }
            else if (a < num)
            {
                cout << "错误,数字偏小" << endl;
            }
            else
            {
                cout << "正确" << endl;
                break;
            }
            c = c + 1;
            cin >> a;
            //}
        }
        system("pause");
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 8月10日
  • 已采纳回答 8月2日
  • 创建了问题 8月2日

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan