【问题标题】:boolean function not working布尔函数不起作用
【发布时间】:2013-12-26 02:36:01
【问题描述】:

我想知道这个逻辑是否正确。该函数通过 compcards[] 获得一个数组,例如 1,2,3,4,5 和一个用户输入的数字,例如 4。但是,如果用户猜到了 6,它将返回 false。我想要求用户五次猜测存储在 compcards[] 中的数字。我想知道我的 if else 语句在这种情况下是否有意义?

bool checkIfCorrect(int checkcard, int compcards[]){
    for ( int i=0; i<5; i++)
    {
        if(compcards[i] == checkcard)
            cout<<"correct"<< endl;
        return true;    
    }
    return false;
}

【问题讨论】:

  • 我只会从函数返回一个值或在屏幕上打印一些东西,避免两者都做。
  • 抱歉,不是很了解。数组“compcards”的目的是什么?它持有什么?用户输入还是正确答案?

标签: c++ arrays function boolean


【解决方案1】:

如下改变你的功能,

bool checkIfCorrect(int checkcard, int compcards[]){
    for ( int i=0; i<5; i++)
    {
        if(compcards[i] == checkcard)
        {       
            cout<<"correct"<< endl;
            return true;    
        }
    }
    return false;
}

当两张牌相等时,您将return true(打印出"correct"后)。在这里,您必须将return 语句包含到if 条件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 2021-06-09
    • 2013-07-22
    • 2013-07-30
    • 2013-08-12
    • 2013-07-03
    相关资源
    最近更新 更多