Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c#菜鸟编程

作者:情丶酸酸甜甜   发布日期:2026-03-04   浏览:82

using System;

namespace CSharpBeginner
{
    class Program
    {
        static void Main(string[] args)
        {
            // 输出 "Hello, World!" 到控制台
            Console.WriteLine("Hello, World!");

            // 定义一个变量并输出
            int number = 10;
            Console.WriteLine("The number is: " + number);

            // 使用 if-else 语句进行条件判断
            if (number > 5)
            {
                Console.WriteLine("Number is greater than 5");
            }
            else
            {
                Console.WriteLine("Number is less than or equal to 5");
            }

            // 使用 for 循环打印数字
            Console.WriteLine("Printing numbers from 1 to 5:");
            for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine(i);
            }

            // 使用 foreach 遍历数组
            string[] names = { "Alice", "Bob", "Charlie" };
            Console.WriteLine("Names in the array:");
            foreach (string name in names)
            {
                Console.WriteLine(name);
            }

            // 获取用户输入
            Console.Write("Please enter your name: ");
            string userName = Console.ReadLine();
            Console.WriteLine("Hello, " + userName + "!");

            // 等待用户按键退出程序
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

解释说明:

  1. Console.WriteLine: 用于向控制台输出信息。
  2. 变量定义: int number = 10; 定义了一个整数类型的变量 number 并赋值为 10。
  3. 条件判断: 使用 if-else 语句来根据条件执行不同的代码块。
  4. 循环结构:
    • for 循环用于重复执行一段代码,这里用来打印从 1 到 5 的数字。
    • foreach 循环用于遍历集合中的每个元素,这里用来遍历字符串数组 names
  5. 用户输入: 使用 Console.ReadLine() 获取用户的输入,并将其存储在变量 userName 中。
  6. 等待用户按键: Console.ReadKey() 用于等待用户按键,防止程序立即关闭。

希望这段代码和解释对你有帮助!

上一篇:c# queue

下一篇:c# func

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站