日期:2014-05-18  浏览次数:20954 次

c#打印出两个菱形在控制台同一高度位置。(就是一左一右)

功能要求
可自定义菱形长度
可自定义菱形个数
可自定义菱形间的间隔
 

程序结果是这样的:
两个菱形在同一水平位置 即一左一右
跪求程序编写方法 代码实现

------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int l = 8; //边长
            int n = 5; //数量
            int x = 3; //间距
            for (int i = 0; i < n / 2; i++)
            {
                for (int j = 0; j < l; j++)
                {
                    string lb = new string(' ', l - j);
                    string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
                    string mb = new string(' ', x + l - j - 1) +lb;
                    Console.WriteLine("{0}{1}{2}{3}", lb, s1, mb, s1);
                }
                for (int j = l - 2; j >= 0; j--)
                {
                    string lb = new string(' ', l - j);
                    string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
                    string mb = new string(' ', x + l - j - 1) + lb;
                    Console.WriteLine("{0}{1}{2}{3}", lb, s1, mb, s1);
                }
                if (i != (n / 2 - 1) || n % 2 == 1)
                {
                    for (int j = 0; j < x; j++)
                    {
                        Console.WriteLine();
                    }
                }
            }
            if (n % 2 == 1)
            {
                for (int j = 0; j < l; j++)
                {
                    string lb = new string(' ', l - j);
                    string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
                    Console.WriteLine("{0}{1}", lb, s1);
                }
                for (int j = l - 2; j >= 0; j--)
                {
                    string lb = new string(' ', l - j);
                    string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
                    Console.WriteLine("{0}{1}", lb, s1);
                }
            }
        }
    }
}

------解决方案--------------------
实心只要把
string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
写成
string s1 = new string('*', j * 2 + 1);
就可以了