【发布时间】:2014-12-10 02:30:15
【问题描述】:
我正在编写经典的“猜我的号码”程序。
我似乎不明白为什么即使在我写了“成功”并转到默认路由之后,while 循环也没有停止工作。
int xRan;
void rdm(int to, int from){
srand(time(NULL));
xRan = rand()%to+from;
}
void iGuess(){
string b;
int tries = 1;
cout << "Think of a number between 1 and 100" << endl;
rdm(100, 1);
while(b != "Success" || b != "success"){
cout << "is your number higher or lower than " << xRan << ". (attempt #" << tries << ")" << endl;
cout << "If I guessed your number, please type 'Success' " << endl;
cout << "-->";
cin >> b;
if(b == "Lower" || b == "lower"){
rdm(xRan, 1);
tries++;
}else if(b == "Higher" || b == "higher"){
rdm(100, xRan);
tries++;
}else{
cout << "This is not a valid choice." << endl;
}
}
cout << "I'm so good! I did it in " << tries << "attempts!" << endl;
}
【问题讨论】:
-
Zeptile,我已经删除了第二个问题“第二个,我想要一些关于编写更好的 AI 来查找号码的建议。”从您的帖子中获取,因为通常将 2 个问题合二为一并不是一个好主意。随时恢复我的更改并自己改进您的问题。您可以将第二个问题作为新问题提出,但请确保搜索类似的帖子,因为您可能不是唯一一个编写此类游戏的人。
标签: c++ while-loop artificial-intelligence