c++,递归函数,循环咋会无效???????

来源:百度知道 编辑:UC知道 时间:2024/05/16 20:45:38
题目是随机产生1到1000的数,让人猜,猜错了给出相应的提示,当输入'y'或'n'时,都不能循环!!!!!!!!!!!!!!!!!!!!!!
#include <iostream>
#include <stdlib.h>
using namespace std;
int a=0, b=0; bool c=true;
bool judge(int ){
if (b==a){
cout<<"Excellent you guessed the number!"<<endl;
cout<<"Would you like to play again (y or n )?";
if ('y'==cin.get())
return true;
else if('n'==cin.get())
return false;}
else if (b>a){
cout<<"Too high.Try again:";
cin>>b;
return judge(b);}
else if(b<a){
cout<<"Too low.Try again:";
cin>>b;
return judge(b);}}
int main(){
srand(time(NULL));
cout<<&

简单作些修改,供楼主参考。
另外,这个不需要递归,用循环就可以的
-------------------------
bool judge(int b)//这里是不是少了b?
{
if (b==a)
{
cout<<"Excellent you guessed the number!"<<endl;
cout<<"Would you like to play again (y or n )?";
//只需要输入一次,所以应该先输入再判断
char ch;
cin>>ch;
if ('y'==ch) return true;
else if('n'==ch) return false;
else return false;//如果不是'y'或者'n',也退出
}
else if (b>a)
{
cout<<"Too high.Try again:";
cin>>b;
return judge(b);}
else if(b<a)
{
cout<<"Too low.Try again:";
cin>>b;
return judge(b);
}
}

原因出在srand(time(NULL))上,如果把这句屏蔽,可以循环,当然就不是随机了
不知道应该怎么修改,改好了再到这联系你

你那个循环是什么用的?
不用循环的~~