Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10308
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 36
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-01 20:31
个人简介

一个苦比逼的程序员!

文章分类
文章存档

2014年(4)

我的朋友

分类: C/C++

2014-01-23 22:05:02

今天在博客文集中看见一段程序,很简单的意图,但根据源程序的逻辑去无法理解,有明显的错误。
现将两段代码贴在此处,已待来日查看。

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <stdlib.h> //used to rand()
  3. using namespace std;

  4. //Please guess 0 - 10 and only 9 is

  5. int main()
  6. {
  7.     int rightnum = 9;
  8.     int usernum;
  9.     char Y_N;
  10.     cout << "Would you roll the wheel ? (Y/N)"<<endl;
  11.     cin >> Y_N;
  12.     while (Y_N == 'Y' || Y_N == 'y')
  13.     {
  14.             usernum = rand() % 11; //rand():0~32765, use % to get 0~10
  15.             if (usernum == 9)
  16.             {
  17.                         cout << "Your number is "
  18.                              << usernum
  19.                              << " and You are lucky !\n"; //It is a
  20.                         break;
  21.                         }
  22.             else
  23.                cout << "Your num is "
  24.                     << usernum <<endl
  25.                     << "Would you roll the wheel again ? (Y/N)"<<endl;
  26.     }
  27.     system("pause");
  28.     return 0;
  29. }

改正后

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <stdlib.h> //used to rand()
  3. using namespace std;

  4. //Please guess 0 - 10 and only 9 is

  5. int main()
  6. {
  7.     int rightnum = 9;
  8.     int usernum;
  9.     char Y_N;
  10.     cout << "Would you roll the wheel ? (Y/N)"<<endl;
  11.     abc:cin >> Y_N;
  12.     while (Y_N == 'Y' || Y_N == 'y')
  13.     {
  14.             usernum = rand() % 110; //rand():0~32765, use % to get 0~10
  15.             if (usernum == 9)
  16.             {
  17.                         cout << "Your number is "
  18.                              << usernum
  19.                              << " and You are lucky !\n"; //It is a
  20.                         break;
  21.                         }
  22.             else
  23.                cout << "Your num is "
  24.                     << usernum <<endl;
  25.                     //<< "Would you roll the wheel again ? (Y/N)"<<endl;
  26.             cout<< "Would you roll the wheel again ? (Y/N)"<<endl;
  27.             goto abc;
  28.     }
  29.     system("pause");
  30.     return 0;
  31. }

阅读(453) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~