首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >数据消毒-猜数字

数据消毒-猜数字
EN

Stack Overflow用户
提问于 2022-10-08 01:58:29
回答 1查看 22关注 0票数 0

我正在编写一个游戏,电脑必须猜出你想要的数字。只有当您第一次输入错误的字符串时,我的数据清理才有效。假设第二次数据清理中断时输入了一个错误的字符串。有人知道怎么解决这个问题吗?

代码语言:javascript
运行
复制
#include <iostream>
#include <string>
using namespace std;

int main() {
    
    string hint, play;
    int max = 100, min = 0, tries=0, num;
    bool again;
    again = true;
    
    while(again = true){
        do {
            num = (max + min) / 2;
            cout<< "The computer's guess is: " << num << endl;
            cout << "Is your number higher, lower, or correct?: ";
            cin >> hint;
            
                if(hint !="lower"){
                    if(hint != "higher"){
                        if(hint!="correct"){
                            cout << "Is your number higher, lower, or correct?: ";
                            cin >> hint; 
                        }
                    }
                }
            
            tries++;
            
            if (hint == "higher")
                min = num + 1;
            else if (hint == "lower")
                max = num - 1;
            else
            cout << "The computer's guess was correct after " << tries<<endl;
            
        }while(hint != "correct");

    cout << "do you want to play again?(y/n): ";
    cin>>play;
    
        if(play!="y"){
            if(play!="n"){
            cout << "do you want to play again?(y/n): ";
            cin>>play;
            }
        }
    
    if(play == "y"){
        again = true;
    }
    if(play == "n"){
        cout<<"thank you for playing";
        break;
    }
    }
}
EN

Stack Overflow用户

发布于 2022-10-08 02:18:13

您需要使用while循环而不是嵌套的if语句。这样,我们就可以等到用户决定输入正确的输入。此外,通常情况下,最好将用户的输入转换为小写,并删除空格,以防他们键入类似于此" CorRecT"的内容。用户将永远不会按一般经验规则准确地键入您期望他们的内容。

我提供的代码需要包含<algorithm>才能正常工作。如果您由于某种原因无法添加它,只需删除以transform开头的行。如果这样做,字符串将不会被删除。

我还将行while (again = true)更改为while (again)。使用=运算符将again的值设置为true,我认为这是无意的。==操作符可能就是你要找的东西。但是,在任何if语句或循环中都没有必要检查true是否相等,因为如果条件等于true,则循环将始终运行。

代码语言:javascript
运行
复制
 while (again) {
        do {
            num = (max + min) / 2;
            cout << "The computer's guess is: " << num << endl; \
            bool invalidInput = true;
            while (invalidInput) {
                cout << "Is your number higher, lower, or correct?: ";
                cin >> hint;
                // Remove whitespace and convert hint to lowercase
                hint.erase(remove_if(hint.begin(), hint.end(), isspace), hint.end());
                transform(hint.begin(), hint.end(), hint.begin(), [](unsigned char c) { return tolower(c); });

                // Validate the input
                if (hint == "higher" || hint == "lower" || hint == "correct") {
                    invalidInput = false;
                }
            }

            ...
    }
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73993893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档