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

How can I include a large number of instruction If in for loops?

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

代码

    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.You不需要使用额外的char数组,可以使用a[index]b[index].

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

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]));
  }

不需要将字符复制到characters1characters2变量中。字符串确实有一个索引器,您可以通过它访问它们的所有字符。

先求出两个字符串的最小长度。我们只能比较较短字符串的长度。

int length = Math.Min(a.Length, b.Length);

现在使用 for 循环遍历字符

for (int i = 0; i < length; i++) {
    ... more to come
}

在循环中你可以使用索引i比较相同位置的字符。取其字符码之差的绝对值。这消除了负号。

int diff = Math.Abs(a[i] - b[i]);

请注意,这是我们唯一一次访问字符。每个都被恰好访问一次。您的代码最多访问每个代码 5 次(复制时,比较 !=、>、<,最后在 Writeline.

如果差值不为0,将结果写入控制台

if (diff != 0) {
    Console.WriteLine(diff);
}

现在大家在一起了

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

就是这样!