首页 > 解决方案 > 增量(x++)的打印结果返回原始值而不是递增一

问题描述

        string text = "hello";

        char[] ch = text.ToCharArray();
        
        for (var x=0; x<text.Length; x++)
        {
            Console.Write(ch[x]++); 
        }

这是我当前的代码,它产生“hello”作为输出。

期望输出为“ifmmp”,因为每个字符都增加 1。

标签: c#charincrement

解决方案


你在同时写入和递增,先尝试递增,然后写入

for (x=0; x<text.Length; x++){
            ch[x]++;
            Console.Write(ch[x]);
        }

推荐阅读