变量的范围大于for循环,为什么它不改变值

Scope of variable larger than for loop, why does it not change value?

本文关键字:为什么 改变 循环 范围 大于 for 变量      更新时间:2023-10-16

我有以下代码:

void nLargestPalindrome(double number){
int i = 5; //subtract "1" because array starts at zero
int intermediate = i; //intermediate value - equal to the array index;
double *decimalDigit = new double[i + 1];
    while (0 <= i){ //acquire digits of number
        decimalDigit[i] = (number - fmod(number, pow(10, i))) / pow(10, i);
        cout << "Value of digit place " << i << " is " << decimalDigit[i] << endl;
        number = fmod(number, pow(10, i));
        i--;
    }

所以我初始化了一个数组,其值等于它所在的数字位置,即 979979 ---> dD[5] = 9,dD[4] = 7,等等......

现在我在同一个函数中有以下代码:

double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];
        for (int q = 0; q < 10; q++){ //decreases size of palindrome to the next largest palindrome.
            cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << 't';
            cout << numberVectorAddition << endl;
            decimalDigit[2]--;
            decimalDigit[3]--;
                if (decimalDigit[2] == 0){
                    cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << 't';
                    cout << numberVectorAddition << endl; //runtime error??
...
}
}

我得到的以下输出number = 997799

Value of digit place 5 is 9
Value of digit place 4 is 9
Value of digit place 3 is 7
Value of digit place 2 is 7
Value of digit place 1 is 9
Value of digit place 0 is 9
997799  997799
996699  997799
995599  997799
994499  997799
993399  997799
992299  997799
991199  997799
990099  997799
989989  997799
988889  997799
987789  997799

什么给?我真诚地尝试在谷歌上环顾四周30分钟,因为我以前在这里遇到过一系列低质量的问题。但我没有成功。

我的问题:因为 numberVectorAddition 大于 for 循环范围,nVA 的值不应该改变吗?

double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];

需要放置在for环内。

    for (int q = 0; q < 10; q++){ //decreases size of palindrome to the next largest palindrome.
        double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];
        cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << 't';
        cout << numberVectorAddition << endl;
        decimalDigit[2]--;
        decimalDigit[3]--;
            if (decimalDigit[2] == 0){
                cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << 't';
                cout << numberVectorAddition << endl; //runtime error??
        ...
}

否则,它将计算一次并保持在该值。

numberVectorAddition不会

自行神奇地改变,无论你如何初始化它,无论你是否使用其他一些可能改变的变量初始化它。一旦numberVectorAddition获得它的值,那就是直到分配给它......在这个代码中,你永远不会这样做!