简体   繁体   English

C# 帮助声明变量我将其初始化为

[英]C# help declaring variable i initialize it to

I'd like to declare an int variable i , initialize it to 4 and then test the following increment and decrement statements.我想声明一个 int 变量i ,将其初始化为 4 ,然后测试以下递增和递减语句。 Comment on the obtained output.评论获得的输出。

Here is the incomplete code that I made:这是我制作的不完整代码:

    class Program
    {
        static void Main(string[] args)
        {
            int Cash;
            Cash = 10;

            Console.WriteLine("{0}", ++ Cash);
            Console.WriteLine("{0}", Cash);
            Console.WriteLine("{0}", Cash ++);
        }
    }

It gives me它给了我

11, 11 11 11, 11 11

from the output.从输出。 I'm not sure If I did it correct.我不确定我是否做对了。 Can someone please correct me if I'm wrong?如果我错了,有人可以纠正我吗?

Yes, the output is correct:是的,输出是正确的:

// This line increments the variable then prints its value.
Console.WriteLine("{0}", ++ Cash);
// This prints the value of the (incremented variable)
Console.WriteLine("{0}", Cash);
// The prints the value of the variable *then* increments its value
Console.WriteLine("{0}", Cash ++);

Using var++ or ++var both increment your var value.使用 var++ 或 ++var 都会增加你的 var 值。 If you use var++ on a writeline, system prints the value of the var before increment it.如果在 writeline 上使用 var++,系统会在增加 var 之前打印 var 的值。

If you want to decrement value from a var, use var--.如果要从 var 递减值,请使用 var--。

When you do ++Cash , it increments the variable first , then prints.当您执行++Cash ,它首先增加变量,然后打印。 After, you just print the variable, then on Cash++ it prints the variable before the increment.之后,您只需打印变量,然后在Cash++它在增量之前打印变量。 So yes your output is correct.所以是的,你的输出是正确的。

++cache= update variable and then take it


cache++ = take value and than update variable
int Cash;
Cash = 10;

Console.WriteLine("{0}", ++ Cash);
Console.WriteLine("{0}", Cash);
Console.WriteLine("{0}", Cash ++);

You initialize Cash to 10 (which should be lower case by the way).您将 Cash 初始化为 10(顺便说一下,它应该是小写的)。 Then you preincrement before the WriteLine() is done.然后在WriteLine()完成之前预递增 Thus it prints 11 .因此它打印11 The next just prints out your cash variable which is at this point 11. Then you do a post increment see link... which prints out the cash variable and then increments it.下一个只是打印出你的cash变量,在这一点上是 11。然后你做一个post increment看到链接......它打印出现金变量然后增加它。 If you writeLine() your cash variable now, it will be 12.如果您现在writeLine()您的cash变量,它将是 12。

++ Cash is "Increase Cash by 1 and give it to me" - it will give you 11 ++ 现金是“将现金增加 1 并给我” - 它会给你 11

Cash then is 11现金则是 11

Cash ++ is "Give me Cash and then increase it by 1" - it will give you 11 and then Cash will be 12. Cash ++ 是“给我现金,然后将其增加 1”-它会给您 11,然后现金将为 12。

Similar question: C# Pre- & Post Increment confusions类似问题: C# 前后增量混淆

You will find alot of valuable information on the outputs you are obtaining here:你会在这里找到很多关于你获得的输出的有价值的信息:

https://msdn.microsoft.com/en-us/library/36x43w8w.aspx https://msdn.microsoft.com/en-us/library/36x43w8w.aspx

Short answer is that you are doing a pre increment and post increment operation there and thus are seeing the result AFTER the operation (in this case addition of one) - the value of the variable currently, and then the result BEFORE the operation.简短的回答是,您在那里进行了前增量和后增量操作,因此会看到操作后的结果(在这种情况下加一) - 当前变量的值,然后是操作前的结果。 This is why you see 11 all three times.这就是为什么你会看到所有 3 次 11。

cheers.干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM