夜雨聆风学习资料网

ARTICLE · 1433809

顺序结构

顺序结构

代码按书写顺序逐行执行,每行代码执行一次,没有跳转或分支,是最基本的程序结构

比如:

using System;

class Program

{

    static void Main()

    {

        // 1. 声明变量

        int score = 85;

        string studentName = "张三";

        // 2. 计算总分(顺序执行)

        int bonusPoints = 5;

        int totalScore = score + bonusPoints;  // 先计算

        // 3. 输出结果(顺序执行)

        Console.WriteLine("学生姓名:" + studentName);

        Console.WriteLine("原始分数:" + score);

        Console.WriteLine("加分:" + bonusPoints);

        Console.WriteLine("最终分数:" + totalScore);

        // 4. 判断是否及格(顺序执行)

        bool isPass = totalScore >= 60;

        Console.WriteLine("是否及格:" + (isPass ? "是" : "否"));

        // 5. 程序结束

        Console.WriteLine("程序执行完毕!");

    }

}

// 计算矩形面积和周长

class Program

{

    // 数据与操作分离

    static double CalculateArea(double length, double width)

    {

        return length * width;  // 纯函数,只依赖输入参数

    }

    static double CalculatePerimeter(double length, double width)

    {

        return 2 * (length + width);

    }

    static void Main()

    {

        // 逐步执行过程

        double length = 5.0;

        double width = 3.0;

        double area = CalculateArea(length, width);

        double perimeter = CalculatePerimeter(length, width);

        Console.WriteLine($"面积: {area}, 周长: {perimeter}");

    }

}

回首页,搜索你想要的文档

在首页搜索框输入关键词,快速定位资料

文库首页搜索指引 立即搜索

相关学习资料