4

プリ デクリメントはいつ使用し、ポスト デクリメントはいつ使用する必要がありますか?

次のコード スニペットでは、プリ デクリメントまたはポスト デクリメントを使用する必要があります。

static private void function(int number)
{
    charArr = new char[number];
    int i = 0;
    int tempCounter;
    int j = 0;
    while(charrArr!=someCharArr)
    {
        tempCounter = number - 1;
        password[tempCounter] = element[i%element.Length];
        i++;
        //This is the loop the I want to use the decrementing in.
        //Suppose I have a char array of size 5, the last element of index 5 is updated
        //in the previous statement.
        //About the upcoming indexes 4, 3, 2, 1 and ZERO.
        //How should I implement it?
        // --tempCounter or tempCounter-- ?
        while (charArr[--tempCounter] == element[element.Length - 1])
        {
        }
    }
}
4

2 に答える 2

5

値が残りの式に渡される前に変数をデクリメントする場合は、プレデクリメントを使用します。一方、post-decrement は、変数が減分される前に式を評価します。

int i = 100, x;
x = --i;                // both are 99

int i = 100, x;
x = i--;                // x = 100, i = 99

インクリメントについても同様です。

于 2010-12-25T22:16:27.250 に答える
0

あなたが持っているべきです++i;(それは重要ではありません)、tempCounter--そうでなければcharArrの「最初の」インデックスを見逃すでしょう

于 2010-12-25T22:12:45.007 に答える