C# 学习日记

学习C# 第一节课,来画个圣诞树助助兴!

你好!C鲨璞! 今天第一节课学习了两句话和一个循环,为了加深记忆所以用输出语句在控制台画个圣诞树练习一下吧!

废话不多说,线上效果图

代码如下

c# 复制代码
namespace hello01    // 命名空间  {} 一层套一层
{
    internal class Program //  Program 类  Program 自定义的 命名规范不能是数字开头,和关键字
    {
        static void Main(string[] args) // 方法 Main 入口
        {
            treeFun(6);
            //tree2Fun(15);
        }

        static void treeFun(int h) {
            // 树身
            for (int i = 1; i <= h; i++)
            {
                string vacancy = "";
                string star = "";
                for (int j = 0; j < h - i; j++)
                {
                    vacancy += " ";
                }
                for (int g = 0; g < 2 * i - 1; g++)
                {
                    star += "*";
                }
                Console.WriteLine(vacancy + star);
            }
            // 树干
            string x = "";
            for (int i = 0; i < (h-1) /1; i++) { 
                    x += " ";
            }
                Console.WriteLine(x +"*" + x);
        }
	
		// 方法2
        static void tree2Fun(int h) {

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < h - i - 1; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 0; k < i * 2 + 1; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }

            // 圣诞树的树干
            for (int i = 0; i < h - 1; i++)
            {
                Console.Write(" ");
            }
            Console.WriteLine("|");
            for (int i = 0; i < h - 1; i++)
            {
                Console.Write(" ");
            }
            Console.WriteLine("|");
        }
    }
}
相关推荐
一阵没来由的风1 小时前
拒绝造轮子(C#篇)ZLG CAN卡驱动封装应用
c#·can·封装·zlg·基础封装·轮子
一枚小小程序员哈7 小时前
基于微信小程序的家教服务平台的设计与实现/基于asp.net/c#的家教服务平台/基于asp.net/c#的家教管理系统
后端·c#·asp.net
Eternity_GQM9 小时前
【Word VBA Zotero 引用宏错误分析与改正指南】【解决[21–23]参考文献格式插入超链接问题】
开发语言·c#·word
cimeo14 小时前
【C 学习】06-算法&程序设计举例
c#
百锦再14 小时前
.NET 的 WebApi 项目必要可配置项都有哪些?
java·开发语言·c#·.net·core·net
WYH2871 天前
C#控制台输入(Read()、ReadKey()和ReadLine())
开发语言·c#
hqwest1 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
做一位快乐的码农1 天前
基于.net、C#、asp.net、vs的保护大自然网站的设计与实现
c#·asp.net·.net
DavieLau1 天前
C#项目WCF接口暴露调用及SOAP接口请求测试(Python版)
xml·服务器·开发语言·python·c#
张人玉1 天前
C#Encoding
开发语言·c#