Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在while循环C++;_C++ - Fatal编程技术网

C++ 如何在while循环C++;

C++ 如何在while循环C++;,c++,C++,使用布尔值为true时退出的while循环的正确语法是什么 我不确定这是否有效: while (CheckPalindrome(a, reverse) == false) { CalcPalindrome(a, reverse); n = a; while (n != 0) { remainder = n % 10; //Finds the 1's digit of n reverse = reverse * 10 + remain

使用布尔值为true时退出的while循环的正确语法是什么

我不确定这是否有效:

while (CheckPalindrome(a, reverse) == false)
{
    CalcPalindrome(a, reverse);
    n = a;
    while (n != 0)
    {
        remainder = n % 10; //Finds the 1's digit of n
        reverse = reverse * 10 + remainder;
        n /= 10;
    }
    CheckPalindrome(a, reverse);
}

您只需要调用一次
CheckPalindrome()
,这就是
while(CheckPalindrome())

另外,正确的语法是
while(!CheckPalindrome())

因此,您的优化代码应该是:

while (!CheckPalindrome(a, reverse))
{
    n = a;

    while (n != 0)
    {
        remainder = n % 10; //Finds the 1's digit of n
        reverse = reverse * 10 + remainder;
        n /= 10;
    }

}

我不确定内部while循环应该做什么,但这是当函数返回
false

时从
while
循环中断的正确语法。您可以将条件缩短为
while(!CheckPalindrome(a,reverse))
但为什么要调用
CheckPalindrome(a,reverse)第二次完全不检查结果?我认为如果您共享
CheckPalindrome
CalcPalindrome
函数而不进一步了解函数的具体功能,我感觉您更希望在(!CheckPalindrome(a,reverse))时使用
do{…}循环。或者递归调用
CheckPalindrome(a,reverse)
。您只需使用逻辑not运算对布尔值求反,它表示为!或者C++中的
not
如何检查结果?