四則運算

Download Report

Transcript 四則運算

Visual Basic 6.0程式設計
程式範例:四則運算
徐雪玲
題目:四則運算
• 內容:設計一程式,在表單視窗可以輸入兩個
變數,並且有四個按鈕分別為加、減、乘、除,
可做四則運算,並顯示運算結果。
所需的物件
物件
屬性
內容
物件
屬性
內容
Form1
Caption
四則運算
Text2
Text
空字串
label1
Caption
變數1:
Text3
Text
空字串
label2
Caption
變數2:
Command1 Caption 加
label3
Caption
運算結果
為:
Text1
Text
空字串
Command2 Caption 減
Command3 Caption 乘
Command4 Caption 除
Val( )函數
• 功用:Val( )函數可以將文字型態的資料
轉換成數字型態。
• 使用時機:輸入Text1、Text2的變數一、
變數二等資料皆為文字型態,所以要進
行算術運算前,必先用Val( )作轉換。
If~Then~Else~EndIf的用法
• 使用時機:用於選擇條件判斷。
• If 條件式 Then
成立敘述
Else
不成立敘述
Endif
程式流程圖
Val(Text2) <> 0
成立
不成立
Text3 = Val(Text1) / Val(Text2)
MsgBox “分母不可為零唷!!"
下一個敘述
總結
• 同學透過這個範例,可以寫出運算式。
• 在往後的程式的範例中,會很常用到
If~Then~Else~Endif 的判斷語法,可
加強學習的印象。
MsgBox指令的用法
• 使用時機:可以出現提示訊息。
• 指令語法:MsgBox 訊息字串
運算式的寫法
•
•
•
•
•
Text3=Val(Text1)+Val(Text2)
Text3=Val(Text1)-Val(Text2)
Text3=Val(Text1)*Val(Text2)
Text3=Val(Text1)/Val(Text2)
注意:運算結果要放在運算式的前面。