如何在for循环中包含大量指令If?

c#

我有一个程序,它返回与两个字符串中相同位置的两个不匹配字符的 ASCII 表的距离。我想缩短我的程序,但以这样一种方式返回与以前相同的结果。(程序不能返回负值,因此它检查每个 If 指令哪个数字具有更大的值并且字符串总是具有相同的长度) 现在程序每次在比较时都会抛出错误:“System.IndexOutOfRangeException: 'Index 超出了数组的边界。” 但它显示正确的结果..

代码

    public static void incompatible(string a, string b)

      {
        char[] characters1 = new char[a.Length];
        char[] characters2 = new char[b.Length];

        for (int i = 0; i < a.Length; i++)
        {
            characters1 [i] = a[i];

        }
        for (int i = 0; i < b.Length; i++)
        {
            characters2 [i] = b[i];

        }



        if (a.Length >= 0 || b.Length >= 0)
        {
            if (characters1 [0] != characters2 [0])
            {
                if (characters1 [0] > characters2 [0])
                {
                    Console.WriteLine(characters1 [0] - characters2 [0]);
                }
                else if (characters1 [0] < characters2 [0])
                {
                    Console.WriteLine(characters2 [0] - characters1 [0]);
                }
            }
        }

        if (a.Length >= 1 || b.Length >= 1)
        {
            if (characters1 [1] != characters2 [1])
            {
                if (characters1 [1] > characters2 [1])
                {
                    Console.WriteLine(characters1 [1] - characters2 [1]);
                }
                else if (characters1 [1] < characters2 [1])
                {
                    Console.WriteLine(characters2 [1] - characters1 [1]);
                }
            }
        }

        if (a.Length >= 2 || b.Length >= 2)
        {
            if (characters1 [2] != characters2 [2])
            {
                if (characters1 [2] > characters2 [2])
                {
                    Console.WriteLine(characters1 [2] - characters2 [2]);
                }
                else if (characters1 [2] < characters2 [2])
                {
                    Console.WriteLine(characters2 [2] - characters1 [2]);
                }
            }
        }

        if (a.Length >= 3 || b.Length >= 3)
        {
            if (characters1 [3] != characters2 [3])
            {
                if (characters1 [3] > characters2 [3])
                {
                    Console.WriteLine(characters1 [3] - characters2 [3]);
                }
                else if (characters1 [3] < characters2 [3])
                {
                    Console.WriteLine(characters2 [3] - characters1 [3]);
                }
            }
        }

我现在有 14 个类似上面的 If 指令。

回答

1.您不需要使用额外的字符数组,您可以使用a[index], b[index]

2.Math.Abs()将返回正差值,而不是像这样的行:

if (characters1 [2] > characters2 [2])
{
      Console.WriteLine(characters1 [2] - characters2 [2]);
}
else if (characters1 [2] < characters2 [2])
{
      Console.WriteLine(characters2 [2] - characters1 [2]);
}

你可以做:

Console.WriteLine(Math.Abs(characters1 [0] - characters2 [0]));

所以关于循环,你的整个代码可以变成这样:

  public static void incompatible(string a, string b)
  {
       for(int i=0; i<Math.Min(a.Length,b.Length);i++)
            if(a[i] != b[i])
                Console.WriteLine(Math.Abs(characters1 [i] - characters2 [i]));
  }


以上是如何在for循环中包含大量指令If?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>