C# 为什么不是';我的RLE的最后一个字符不显示吗?

C# 为什么不是';我的RLE的最后一个字符不显示吗?,c#,run-length-encoding,C#,Run Length Encoding,我是C#新手,尝试编写自己的简单方法,对字符串进行游程编码。除最后一个字母外,它工作正常,因为最后一个字母不会显示。我的逻辑有什么问题 namespace RunLengthEncoding { class Program { static void Main(string[] args) { string tobesorted; string encoded = ""; in

我是C#新手,尝试编写自己的简单方法,对字符串进行游程编码。除最后一个字母外,它工作正常,因为最后一个字母不会显示。我的逻辑有什么问题

namespace RunLengthEncoding
{
    class Program
    {
        static void Main(string[] args)
        {
            string tobesorted;
            string encoded = "";
            int temp1, temp2;
            int same = 1;


            Console.WriteLine("Please enter the string you want to be sorted");
            tobesorted = Console.ReadLine();
            tobesorted = tobesorted.ToUpper();
            tobesorted = tobesorted.Replace(" ", string.Empty);
            char[] tbsarray = tobesorted.ToCharArray();
            for (int i =0; i < tbsarray.Length-1; i++)
            {

                temp1 = tbsarray[i];
                temp2 = tbsarray[i + 1];
                if (temp1==temp2)
                {
                    same++;
                }
                else
                {
                    encoded = encoded + tbsarray[i];
                    encoded = encoded + Convert.ToString(same);
                    same = 1;
                }
                if ((tbsarray.Length-2 == i))
                {
                    encoded = encoded + tbsarray[i] + Convert.ToString(same);
                    Console.WriteLine(encoded);
                }
            }
            Console.WriteLine(encoded);
            Console.ReadLine();

        }
    }
}
名称空间运行编码
{
班级计划
{
静态void Main(字符串[]参数)
{
要插入的字符串;
字符串编码=”;
int temp1,temp2;
int-same=1;
WriteLine(“请输入要排序的字符串”);
tobesorted=Console.ReadLine();
tobesorted=tobesorted.ToUpper();
tobesorted=tobesorted.Replace(“,string.Empty);
char[]tbsarray=tobesorted.ToCharArray();
对于(int i=0;i
要插入的字符串;
字符串编码=”;
int temp1
int-same=1;
WriteLine(“请输入要排序的字符串”);
tobesorted=Console.ReadLine();
tobesorted=tobesorted.ToUpper();
tobesorted=tobesorted.Replace(“,string.Empty);
char[]tbsarray=tobesorted.ToCharArray();
对于(int i=0;i
能否提供一个输入/输出示例,包括您当前获得的结果和预期结果?假设数组中的最后一个字符与前一个不同。请注意,根据您的循环,绝不可能将
tbsarray[tbsarray.Length-1]
添加到输出中。您可以尝试以另一种方式执行操作,这样做会做得更好—使用一个包含仍在等待输出的字符的变量,初始化
tbsarray[0]
,然后从
1
运行到
tbsarray.Length-1
的循环。aaaaaabbbbbbcccc将给出a6b6不确定temp2在做什么,但是我为您格式化了它,所以它会遍历所有内容。temp2将字符i+1存储在数组中,然后比较这两个字符是否相同
        string tobesorted;
        string encoded = "";
        int temp1
        int same = 1;

        Console.WriteLine("Please enter the string you want to be sorted");

        tobesorted = Console.ReadLine();
        tobesorted = tobesorted.ToUpper();
        tobesorted = tobesorted.Replace(" ", string.Empty);
        char[] tbsarray = tobesorted.ToCharArray();
        for (int i = 0; i < tbsarray.Length; i++)
        {
            temp1 = tbsarray[i];

            encoded = encoded + tbsarray[i];
            encoded = encoded + Convert.ToString(same);
            same = 1;

            if ((tbsarray.Length - 2 == i))
            {
                encoded = encoded + tbsarray[i] + Convert.ToString(same);
                Console.WriteLine(encoded);
            }
        }

        Console.WriteLine(encoded);
        Console.ReadLine();