首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

工作流程式設計漸進指南(三):While 活動實作九九乘法表

     編輯:關於C#

一、While 活動簡介

While 活動持續執行直到條件為假。本範例實作九九乘法表雙重迴圈。

二、建立專案

  1. VS2008 新增控制台循序工作流,命名 WhileActivity。
  2. 拖入 While 活動。
  3. 拖入 While 活動

三、巢狀迴圈設計

直接巢狀 While 無法處理內層變數重置,解決方案:外層 While 內放入 Sequence(串行)活動,Sequence 內再放入內層 While 與重置程式碼。

巢狀 While 結構

四、變數與條件設定

工作流類別變數

private int i = 0;
private int j = 1;

外層 While 條件

規則條件:this.i <= 9

規則條件編輯器 條件運算式 新增規則對話框

內層 While 條件

規則條件:this.j <= this.i

五、Code 活動程式碼

private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
    j = 1;
    i++;
}

private void codeActivity2_ExecuteCode(object sender, EventArgs e)
{
    if (i == j)
    {
        Console.Write("{0} * {1}={2} ", i, j, i * j);
        Console.WriteLine("");
    }
    else
        Console.Write("{0} * {1}={2} ", i, j, i * j);
    j++;
}

private void codeActivity3_ExecuteCode(object sender, EventArgs e)
{
    Console.Read();
}

六、完整工作流類別程式碼

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

namespace WhileActiviity
{
    public sealed partial class Workflow1 : SequentialWorkflowActivity
    {
        private int i = 0;
        private int j = 1;

        public Workflow1() { InitializeComponent(); }

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            j = 1; i++;
        }

        private void codeActivity2_ExecuteCode(object sender, EventArgs e)
        {
            if (i == j)
            {
                Console.Write("{0} * {1}={2} ", i, j, i * j);
                Console.WriteLine("");
            }
            else
                Console.Write("{0} * {1}={2} ", i, j, i * j);
            j++;
        }

        private void codeActivity3_ExecuteCode(object sender, EventArgs e)
        {
            Console.Read();
        }
    }
}

七、執行結果

九九乘法表輸出

出處

https://zhuxianzhong.blog.51cto.com/157061/203304

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved