今天在博客文集中看见一段程序,很简单的意图,但根据源程序的逻辑去无法理解,有明显的错误。
现将两段代码贴在此处,已待来日查看。
-
#include <iostream>
-
#include <stdlib.h> //used to rand()
-
using namespace std;
-
-
//Please guess 0 - 10 and only 9 is
-
-
int main()
-
{
-
int rightnum = 9;
-
int usernum;
-
char Y_N;
-
cout << "Would you roll the wheel ? (Y/N)"<<endl;
-
cin >> Y_N;
-
while (Y_N == 'Y' || Y_N == 'y')
-
{
-
usernum = rand() % 11; //rand():0~32765, use % to get 0~10
-
if (usernum == 9)
-
{
-
cout << "Your number is "
-
<< usernum
-
<< " and You are lucky !\n"; //It is a
-
break;
-
}
-
else
-
cout << "Your num is "
-
<< usernum <<endl
-
<< "Would you roll the wheel again ? (Y/N)"<<endl;
-
}
-
system("pause");
-
return 0;
-
}
改正后
-
#include <iostream>
-
#include <stdlib.h> //used to rand()
-
using namespace std;
-
-
//Please guess 0 - 10 and only 9 is
-
-
int main()
-
{
-
int rightnum = 9;
-
int usernum;
-
char Y_N;
-
cout << "Would you roll the wheel ? (Y/N)"<<endl;
-
abc:cin >> Y_N;
-
while (Y_N == 'Y' || Y_N == 'y')
-
{
-
usernum = rand() % 110; //rand():0~32765, use % to get 0~10
-
if (usernum == 9)
-
{
-
cout << "Your number is "
-
<< usernum
-
<< " and You are lucky !\n"; //It is a
-
break;
-
}
-
else
-
cout << "Your num is "
-
<< usernum <<endl;
-
//<< "Would you roll the wheel again ? (Y/N)"<<endl;
-
cout<< "Would you roll the wheel again ? (Y/N)"<<endl;
-
goto abc;
-
}
-
system("pause");
-
return 0;
-
}
阅读(494) | 评论(0) | 转发(0) |