[总结]机房收费系统 条件判断

来源:互联网 发布:淘宝自己开店套现 编辑:程序博客网 时间:2024/06/04 23:24

前言:在机房收费系统中,我们遇到最多的不是bug,而是  选择!

1、if语句

(1)行if 语句(占一个语句行,没有end if )

格式

if 条件 then 语句组1 [else 语句2]例如:if x>0 then print x Else print –x;(如果饿了,吃饭;[不饿,不吃,反正就是不吃饭]。

(2)块if语句(注意格式) If 条件then
     [语句组1][Else     [语句组2]End if 

先判断条件是否成立,成立,执行语句组1,不成立,执行语句组2.</span>     If x>0 then     Print x     Else    Print-x     End if 


(个人认为,块if比行if区别在,块if较复杂,要分段,有end if 。在块if中又可以嵌套,可以添加行if 或者再添加块if,块if 是一个框架。)

2、使用elseif

块 if中可以使用elseif字句,语法如下:

<span style="font-size:24px;">If 条件1 then   [语句组1][Elseif 条件2 then[语句组2][Elseif 条件3] then[语句组3] ……[else        <[语句组n]]End if</span>



例如

<span style="font-family: FangSong_GB2312; font-size: 18px; background-color: rgb(255, 255, 255);">等同于</span>

<span style="font-size:24px;">Sub form_click()<span style="white-space:pre"></span>Dim I as intenger<span style="white-space:pre"></span>If i=1 then<span style="white-space:pre"></span>Form1.show<span style="white-space:pre"></span>Elseif i=2 then<span style="white-space:pre"></span>Form2.show<span style="white-space:pre"></span>Elseif i=3 then<span style="white-space:pre"></span>Unload me <span style="white-space:pre"></span>End if (只有一个if 对应着end if )End sub</span>

<span style="font-size:24px;">Sub form_click()If i=1 then<span style="white-space:pre"></span>Form1.show<span style="white-space:pre"></span>Else<span style="white-space:pre"></span>If i=2 then<span style="white-space:pre"></span>Form2.show<span style="white-space:pre"></span>Else<span style="white-space:pre"></span>If i=3 then<span style="white-space:pre"></span>Unload me<span style="white-space:pre"></span>End if<span style="white-space:pre"></span>End if<span style="white-space:pre"></span>End if(很多嵌套,比elseif复杂了很多) End sub</span>




Elseif 的使用简化了很多嵌套,格式整齐。

3、selec case语句(用于构造多分支选择结构)

表达式

<span style="font-size:24px;">select case[case 表达式列表1[语句块1]][case 表达式列表2[语句块2]]……Case else[语句块]End select </span>
Select case 语句执行时,首先计算测试表达式的值,然后用该值依次测试各个表达式列表,如果在某个列表中找到匹配的表达式,则执行该case语句之后的语句块,然后执行end select语句之后的语句。如果所有的表达式都不能匹配,执行case else后的句子。
常见形式:


<span style="font-size: 18px; font-family: FangSong_GB2312; background-color: rgb(255, 255, 255);">(1)</span><span style="font-size: 18px; font-family: FangSong_GB2312; background-color: rgb(255, 255, 255);"></span><span style="font-size: 18px; font-family: FangSong_GB2312; background-color: rgb(255, 255, 255);">单独的常量。 Case1,3,5,”a”,”b”</span>
(2)用to 制定范围。例如,case 2 to 10,”abc”to “xyz”
(3) 用is指定条件。 Case is<60,is >90.
(4) 前面三种混合。Case 1,3,5,10 to 20,is >60
见机房中结账时用sstab时的选择。(部分代码)
<span style="font-size:24px;">Select Case SSTab.Tab    Case 0 '购卡       txtsql = "select * from student_info where UserID='" & Trim(cbuserid.Text) & "'and Ischeck='未结账'"       Set mrc = ExecuteSQL(txtsql, msgtext)       With salecardms      .Rows = 1       .CellAlignment = 4   Do While Not mrc.EOF            .Rows = .Rows + 1            .TextMatrix(.Rows - 1, 0) = mrc.Fields(1).Value            mrc.MoveNext         Loop       End With         </span>

<span style="font-size:24px;">Case 1 '充值         txtsql = "select * from Recharge_info where UserID='" & Trim(cbuserid.Text) & "'and status='未结账'"       Set mrcc = ExecuteSQL(txtsql, msgtext)       With rechargems           .Rows = 1         .CellAlignment = 4    Do While Not mrcc.EOF           .Rows = .Rows + 1         .TextMatrix(.Rows - 1, 0) = mrcc.Fields(1).Value            mrcc.MoveNext           Loop       End With</span>

<span style="font-size:24px;">Case 2 '退卡txtsql = "select * from CancelCard_info where UserID='" & Trim(cbuserid.Text) & "'and status='未结账'"        Set mrccc = ExecuteSQL(txtsql, msgtext)        With cancelcardms           .Rows = 1           .CellAlignment = 4            Do While Not mrccc.EOF           .Rows = .Rows + 1            .TextMatrix(.Rows - 1, 0) = mrccc.Fields(0).Value           mrccc.MoveNext            Loop       End With</span><span style="font-size:24px;">Case 3  '临时用户 等等end select      </span>


    用好if 和end if不仅是一个技术活,更是一个完整思维的体现,在敲机房利用循环的过程中,我出现了很多次“在对象关闭时不允许操作”的问题,是在对象关闭后(mrc.close)相当于释放了变量,这次查询结束,找不到内容了,不能再执行和这个记录集有关的操作。造成这个问题的一个操作就是将end if放错位置,没确定在哪里结束。
(为了不丢 end if,可以在每次都写一对(if end if),然后向其中添加内容。)
(唉唉  之前格式不对,真是对不起读者了!)



0 0
原创粉丝点击