C#通过键盘输入行列数,输出空心的矩形

2025-05-11 14:52:01
推荐回答(2个)
回答1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Star
{
    class Program
    {
        static void Main(string[] args)
        {
            rectangle(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()));
            Console.Read();
        }

        static void rectangle(int h,int w)
        {
            string star = "*";
            for (int i = 0; i < h; i++)
            {
                if (i == 0 || i == (h - 1))
                    Console.Write(star.PadLeft(w , '*'));
                else
                { 
                    Console.Write(star);
                    Console.Write("".PadLeft(w - 2, ' '));
                    Console.Write(star);
                }
                Console.WriteLine();
            }
        }
    }
}

回答2:

是控制台程序嘛