Skip to main content
 首页 » 编程设计

c#之增量和减量的总量

2024年02月23日18softidea

namespace rojak2.cs 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            ArithmeticOperators(); 
        } 
 
        static void ArithmeticOperators() 
        { 
            double totalAmount = 100; 
            double result; 
 
            Console.WriteLine("totalAmount is {0}", totalAmount); 
            Console.WriteLine(); 
 
            result = totalAmount + 100; 
            Console.WriteLine("totaAmount is {0}", result); 
 
            result = totalAmount - 50; 
            Console.WriteLine("totaAmount is {0}", result); 
 
            result = ++totalAmount; 
            Console.WriteLine("totaAmount is {0}", totalAmount); 
 
            result = --totalAmount; 
            Console.WriteLine("totaAmount is {0}", totalAmount); 
        } 
    } 
 
} 

我的问题是为什么最后输出的result是100而不是99?它应该从 100 而不是 101 减少。我不太明白。

请您参考如下方法:

因为预增量。变量值在其值被复制到结果之前递增。因此 result 将有 101 作为预递增和递减的结果 - 它首先减去一个然后复制值,因此你得到的结果为 100。