用C#实现 循环:空心菱形与实心菱形,三角形

  • 三元表达式(三目运算):

语法:x>y?z:k

例如:

     int int_num1 = 10;

            int int_num2 = 20;

            string str1 = "我是z";

            string str2 = "我是k";

            string result = int_num1 > int_num2 ? str1 : str2;

            Console.WriteLine(result);

   Console.ReadKey();

  • for循环
    1. 冒泡排序

 int[] arr_int = { 55,78,3,99,56};

            for (int i = 0; i < arr_int.Length; i++)

            {

                for (int j = 0; j < arr_int.Length; j++)

                {

                    if (arr_int[i]>arr_int[j])

                    {

                        int temp = arr_int[i];

                        arr_int[i] = arr_int[j];

                        arr_int[j] = temp;

                    }

                }

            }

            for (int i = 0; i < arr_int.Length; i++)

            {

                Console.WriteLine(arr_int[i]);

            }

    1. 三角形

            for (int i = 0; i < 10; i++)

            {

                for (int j = 0; j < 10; j++)

                {

                    if (i < j)

                    {

                        Console.Write("*");

                    }

                    else

                    {

                        Console.Write(" ");

                    }

                }

                Console.WriteLine();

            }

  1. 空心菱形

for (int i = 0; i <= 10; i++)

            {

                for (int j = 0; j <= 10; j++)

                {

                    if (i == -j + 5 || i == -j + 15 || i == j + 5 || i == j - 5)

                    {

                        Console.Write("*");

                    }

                    else

                    {

                        Console.Write(" ");

                    }

                }

                Console.WriteLine();

            }

  1. 实心菱形

for (int i = 0; i <= 10; i++)

            {

                for (int j = 0; j <= 10; j++)

                {

                    if (i >= -j + 5 && i <= -j + 15 && i <= j + 5 && i >= j - 5)

                    {

                        Console.Write("*");

                    }

                    else

                    {

                        Console.Write(" ");

                    }

                }

                Console.WriteLine();

            }

5、var类型推断,使用var必须赋初值,一但赋值,数据类型就确定了

你可能感兴趣的:(用C#实现 循环:空心菱形与实心菱形,三角形)