簡體   English   中英

比較字符總是返回true

[英]comparing character always returns true

為什么hasParenthesis總是評估為true?

bool hasParenthesis = false ;
for(int i = 0; i < 255 && statement[i] != ';'; i++)
{
  if(statement[i] == '(' || statement[i] == ')')
  {
    hasParenthesis = true;
    break;
  }
}

服務員,我的循環中有一個if!

假設statement是一個std::string ,你可以擺脫兩者:

auto pos = statement.find_first_of(";()");
bool hasParenthesis = (pos != std::string::npos) && (statement[pos] != ';');

當for循環開始時,將hasParenthesis設置為false。 使用你現在擁有的東西,一旦布爾值為真,當循環重新迭代時它總是成立。 因此,使用布爾值false啟動for循環邏輯。

這是一個簡化的骨架:

bool hasParenthesis;
for(){
    hasParenthesis = false;

    if(){
      hasParenthesis = true;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM