如何将 if else 语句放入循环中

这是我的代码。到目前为止,这是有效的,但我需要它处于循环中,所以我不会一直重复该if else语句。


static void Main(string[] args)

        {

            int i, j, k, l, m, n;

            int result;

            string [] array = { "abcw", "baz", "foo", "bar", "xtfn", "abcdef" };

            i = array[0].Length;

            j = array[1].Length;

            k = array[2].Length; 

            l = array[3].Length;

            m = array[4].Length;

            n = array[5].Length;

            result = i * j;

            if (result == 16) 

            {

                Console.WriteLine(result);

            }

            else

            {

                result = i * k;

            }

            if (result == 16)

            {

                Console.WriteLine(result);

            }

            else

            {

                result = i * l;

            }

            if (result == 16)

            {

                Console.WriteLine(result);

            }

            else

            {

                result = i * m;

            }

            if (result == 16)

            {

                Console.WriteLine(array[0]+" * "+array[4]+" = "+result);

            }

            else

            {

                result = i * n;

            }


米脂
浏览 215回答 3
3回答

智慧大石

如果您创建一个循环遍历所有条目的外循环,然后创建一个循环遍历您在外循环中查看的条目之后的条目的内循环,您可以执行以下操作&nbsp; &nbsp; &nbsp; string[] array = {"abcw", "baz", "foo", "bar", "xtfn", "abcdef"};&nbsp; &nbsp; &nbsp; for (var i = 0; i < array.Length; i++)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var j = i + 1; j < array.Length; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[i].Length * array[j].Length == 16)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"{array[i]} {array[j]}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }然后你得到的结果是 abcw xtfn

手掌心

缩短代码的可能方法之一。您可以迭代您的数组并将第一项与您在问题代码块中提到的其余项进行比较。无需访问单个数组元素。将第一个元素与其余元素进行比较static void Main(string[] args){&nbsp; &nbsp; int result;&nbsp; &nbsp; string[] array = { "abcw", "baz", "foo", "bar", "xtfn", "abcdef" };&nbsp; &nbsp; for (int i = 0; i < array.Length - 2; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; result = array[0].Length * array[i + 1].Length;&nbsp; &nbsp; &nbsp; &nbsp; if (result == 16)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(array[0] + " * " + array[i+1] + " = " + result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Console.ReadLine();}比较中间的所有元素for (var i = 0; i < array.Length; i++)&nbsp; &nbsp; for (var j = i + 1; j < array.Length; j++)&nbsp; &nbsp; &nbsp; &nbsp; if (array[i].Length * array[j].Length == 16)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(array[i] + " * " + array[j] + " = " + array[i].Length * array[j].Length);

素胚勾勒不出你

string[] array = { "abcw", "baz", "foo", "bar", "xtfn", "abcdef" };&nbsp; &nbsp; &nbsp; &nbsp; int result = 16;&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < array.Length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x = array[i].Length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result % x != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var j = i + 1; j < array.Length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (x * array[j].Length == result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(array[i] + "*" + array[j] + "= " + result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP