除法VB程序编码
VB计算器(界面设计全部代码)
VB计算器此计算器能够实现加减乘除等一系列的功能。
还有实现显示系统时间的附加功能。
程序设计:0-9和点在command1的组内。
其他的按钮式运算符和其他功能。
正切和余切有对特殊值进行警告,除也有对除数为零的情况提示警告,还有阶乘等一系列计算溢出的情况提出警告,还有很多需要完善。
这仅供参考。
Public sum As DoublePublic k As Stri ngPublic dia n As Boolea nPublic b As In tegerPublic poin tflag As Boolea nDim clearFlag As Boolea nPublic F As Long'Dim start As boole nPublic resl As Boolean ' 运算结果存储在resl里数字按钮在一个组内,点Public ff As Double 'resl 里是否为空'llllllllllllllllllllllllllllllllllllllllllllllllllllllll击按钮输入数字 lllllllllllllllllllllllllllllllllllllllllllPrivate Sub comma nd1_click(l ndex As In teger) Select Case In dex Case 1If Not clearFlag The n ' 不是等号,那么显示 1 Text1.Text = Text1.Text & 1 Else'是等号,那么清空Text1.Text = 1 clearFlag = False End IfIf Len(Text1.Text) = 2 And InStr(1, Text1, "0") = 1 Then Text1 = Right(Text1, 1) '如果第一位的数位 0,字符串的长度为 2.那么取字符串右面的数,即两个数中右面的数Case 2If Not clearFlag Then Text1.Text = Text1.Text & 2 ElseText1.Text = 2 clearFlag = False End IfIf Len (Text1.Text) = 2 And In Str(1, Text1, "0") = 1 The n Text1 : Case 3 If Not clearFlag The n Text1.Text = Text1.Text & 3 Else=Right(Text1, 1)Text1.Text clearFlag = End If=3 False If Len (Text1.Text) = 2 And In Str(1, Text1, "0") = 1 The n Text1 : =Right(Text1, 1)Case 4If Not clearFlag The nText1.Text =Text1.Text & 4ElseText1.Text =4clearFlag = FalseEnd IfIf Len (Text1.Text) = 2 And In Str(1, Text1, "0") = 1 The n Text1 : =Right(Text1, 1)Case 5If Not clearFlag ThenTextl.Text = Textl.Text & 5 ElseTextl.Text = 5 clearFlag = False End IfIf Len(Text1.Text) = 2 And InStr(1, Text1, "0")= =1 The n Text1 ==Right(Text1, 1)Case 6If Not clearFlag The nText1.Text =Text1.Text & 6ElseText1.Text =6clearFlag = End IfFalseIf Len(Text1.Text) = 2 And InStr(1, Text1, "0")= =1 The n Text1= =Right(Text1, 1)Case 7If Not clearFlag The n Text1.Text = Text1.Text & 7 ElseTextl.Text = 7 clearFlag = False End IfIf Len(Textl.Text) = 2 And InStr(1, Textl, "0") = 1 Then Textl = Right(Text1, 1) Case 8If Not clearFlag The n Textl.Text = Textl.Text & 8 ElseTextl.Text = 8 clearFlag = False End IfIf Len(Textl.Text) = 2 And InStr(1, Textl, "0") = 1 Then Textl = Right(Text1, 1) Case 9If Not clearFlag The n Text1.Text = Text1.Text & 9 ElseText1.Text = 9 clearFlag = False End IfIf Len(Text1.Text) = 2 And InStr(1, Text1, "0") = 1 Then Text1 = Right(Text1, 1) Case 0定义加号按钮 //////////////////////////////////////If Not clearFlag Then Textl.Text = Textl.Text & 0 ElseText1.Text = 0 clearFlag = False End IfIf Len(Text1.Text) = 2 And InStr(1, Text1, "0") = 1 Then Text1 = Right(Text1, 1) Case 10If Not clearFlag The n Text1.Text = Text1.Text + "." ElseText1.Text ="" clearFlag = False End If7///////////////////////////////////////////////// 对输入点 的个数进行约 束///////////////////////////////////////////////////////////If (In Str(Text1.Text, ".")= =1) The n 对点的处理,检查是否有点,有点再输入的话为空,否则加入其中Textl.Text =""End IfIf InStr(Text1.Text, ".") < Len(Textl.Text) Then Textl.Text = Left(Text1.Text, Len(Textl.Text) - 1) End IfEnd SelectText1.SetFocus End Sub '////////////////////////Private Sub comma nd2_click() If Text1.Text = "" The n End IfIf Trim(Text1.Text) <> "" Then sum = Text1.Text Text1.Text ="" k = "+" Else Exit Sub End If End Sub 7////////////////////////定义减号按钮 ////////////////////////////////////// Private Sub comma nd3_click()定义除号按钮If Text1.Text = "" Then End IfIf Trim(Text1.Text) <> "" The n sum = Text1.Text ' 第一个数字填入方框里 Text1.Text =""' 方框的值置空k ="-" Else Exit Sub End If End Sub '///////////////////////定义乘号按钮 ////////////////////////////////////////Private Sub comma nd4_click()If Text1.Text = "" The n End IfIf Trim(Text1.Text) <> "" Then sum = Text1.Text Text1.Text ="" k = "*" Else Exit Sub End If End Sub '/////////////////////////////////////////////////////////////////// Private Sub comma nd5_click() If Text1.Text = "" Then End IfIf Trim(Text1.Text) <> "" Then sum = Text1.Text Text1.Text ="" k = "/" Else Exit Sub End If End Sub'IIIIIIIIIIIIIIIIIIIIIIIIIIII 定 义sin号 按钮/////////////////////////////////////Private Sub comma nd1O_click() If Text1.Text = "" Then End IfIf Trim(Text1.Text) <> "" The n sum = Text1.TextElseExit SubEnd IfEnd Sub'///////////////////////////// 定义cos 按钮/////////////////////////////////////Private Sub comma nd11_click()If Text1.Text = "" The nEnd IfIf Trim(Text1.Text) <> "" The nsum = Text1.Textk ="cos"ElseExit SubEnd IfEnd Sub'///////////////////////////// 定义tan 按钮////////////////////////////////////Private Sub comma nd12_click()If Text1.Text = "" The nEnd IfIf Trim(Text1.Text) <> "" The nsum = Text1.Textk = "tan"ElseExit SubEnd IfEnd Sub'////////////////////////// 定义cot 按钮//////////////////////////////////////Private Sub comma nd13_click()If Text1.Text = "" The nEnd IfIf Trim(Text1.Text) <> "" Then sum = Textl.Textk = "cot"ElseExit SubEnd IfEnd Sub7////////////////////////// 定义人按钮///////////////////////////////////////Private Sub Comma nd14_click()If Textl.Text = "" The nExit SubIf Trim(Textl.Text) <> "" The nsum = Textl.TextTextl.Text =""k = "A"ElseExit SubEnd IfEnd Sub'///////////////////////// 定义2A ///////////////////////////////////////////Private Sub comma nd15_click()If Textl.Text = "" The nExit SubEnd IfIf Trim(Textl.Text) <> "" The nsum = Textl.Textk = "2人"ElseExit SubEnd IfEnd Sub'/////////////////////// 定义3A 按钮/////////////////////////////////////////// Private Sub comma nd16_click()If Textl.Text = "" The nExit SubEnd IfIf Trim(Textl.Text) <> "" The nsum = Textl.Textk = "3A"ElseExit SubEnd IfEnd Sub'IIIIIIIIIIIIIIIIIIII定义X!按钮///////////////////////////////////////////// Private Sub comma nd17_click()If Text1.Text = "" The nExit SubEnd IfIf Trim(Text1.Text) <> "" The nsum = Text1.Textk = "x!"ElseExit SubEnd If'////////////////// 定义sqrt 按钮////////////////////////////////////////////// Private Sub comma nd18_click()If Text1.Text = "" The nExit SubEnd IfIf Trim(Text1.Text) <> "" The nsum = Text1.Textk = "sqrt"ElseExit SubEnd IfEnd Sub'///////////////////////////// 定义阶乘的函数////////////////////////////////// Private Fu nction Ni(N As Long) As Boolea n 'Ni = FalseDim Mm As Long 'Dim F1 As Lo ngF = 1For Mm = 1 To NIf Mm > 12 The n 'If F1 > The n Ni = False Exit Fun cti on End IfF = F * MmNext MmNi = TrueEnd FunctionPrivate Sub equalbut_click() clearFlag = FalseEnd SubPrivate Sub comma nd6_click()If Trim(Text1.Text) <> "" Then 'Dim F As DoubleDim a As DoubleDim pi As DoubleDim i, j As In tegerpi = 3.14159265358979If Text1.Text > 2147483647 ThenMsgBox "溢出,请确认", vbExclamation, Me.CaptionExit SubEnd If a = Text1.Text clearFlag = TrueIf k = "+" Then sum = sum + a Text1.Text = sumElseIf k = "s in" The n sum = Sin(a * pi / 180) Textl.Text = sumElseIf k = "cos" ThenDim t As In tegert = a Mod 180 - 90If t = 0 ThenText1.Text = 0If m = 0 The n MsgBox " ElseElsesum = Cos(a * pi / 180) Textl.Text = sum End IfElseIf k = "tan" Then Dim m As In teger Dim N As In teger m = a Mod 180 - 90(90+180*n )度的正切值无意义,请重新输入 sum = (Tan(a * pi / 180)) Textl.Text = sum End IfElseIf k = "cot" The n If a = 0 The n MsgBox "0 度余切没有意义!请重新输入!Elsesum = 1 / (Tan(a * pi / 180)) Textl.Text = sum End If'ElseIf k = "x!" The n 'If Text1.Text > 0 The n 'Call fact(Text1.Text, F) ' Text1.Text = F'ElseIf Text1.Text = 0 The n 'sum = 1' Text1.Text = sum'ElseIf Text1.Text < 0 The n 'MsgBox " 负数没有阶乘! ”'End If减法运算Elself k = "x!" The nIf Ni(Textl.Text) = False The nMsgBox "溢出", vbExclamation, Me.CaptionText1.Text =""Exit SubElseText1.Text = FEnd IfElself k = "2A " The nTextl.Text = Val(Textl.Text) * Val(Textl.Text)Elself k = "3A" The nTextl.Text = Val(Textl.Text) * Val(Textl.Text) * Val(Textl.Text) Elself k = "sqr" The nIf a >= 0 The nsum = Math.Sqr(a)Textl.Text = sumElseMsgBox "开方数不能为负数!”End If Elself k = "A" The n sum =sum A a Textl.Text = sum Elself k = "-" The n sum = sum - a Textl.Text = sum resl = 1Elself k = "*" The n sum = sum * a Textl.Text = sum Elself k = "/" The nIf Textl.Text = 0 The nMsgBox "除数不能为零!请重新输入Textl.Text =""Elsesum = sum / aTextl.Text = sumIf Len( Textl.Text) > 14 The nMsgBox "溢出,请确认", vbExclamation, Me.Caption Exit SubEnd IfExit SubEnd IfEnd IfEnd IfEnd SubPrivate Sub Comma nd9_Click()If Len(Text1.Text) >= 2 ThenText1.Text = Left(Text1.Text, Len(Text1.Text) - 1) ElseText1.Text =""End IfEnd SubPrivate Sub form」。
VB简易计算器代码
VB简易计算器代码以下是一个简单的VB计算器代码:```Public Class Form1Dim firstNum As Double ' 第一个数字Dim secondNum As Double ' 第二个数字Dim operation As Integer ' 1-加法,2-减法,3-乘法,4-除法Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.ClickTextBoxResult.Text = TextBoxResult.Text & "0"End SubPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickTextBoxResult.Text = TextBoxResult.Text & "1"End SubPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickTextBoxResult.Text = TextBoxResult.Text & "2"End SubHandles Button3.ClickTextBoxResult.Text = TextBoxResult.Text & "3"End SubPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickTextBoxResult.Text = TextBoxResult.Text & "4"End SubPrivate Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.ClickTextBoxResult.Text = TextBoxResult.Text & "5"End SubPrivate Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.ClickTextBoxResult.Text = TextBoxResult.Text & "6"End SubPrivate Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.ClickTextBoxResult.Text = TextBoxResult.Text & "7"End SubHandles Button8.ClickTextBoxResult.Text = TextBoxResult.Text & "8"End SubPrivate Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.ClickTextBoxResult.Text = TextBoxResult.Text & "9"End SubPrivate Sub ButtonDot_Click(sender As Object, e As EventArgs) Handles ButtonDot.ClickIf Not TextBoxResult.Text.Contains(".") ThenTextBoxResult.Text = TextBoxResult.Text & "."End IfEnd SubPrivate Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 1End SubPrivate Sub ButtonSubtract_Click(sender As Object, e As EventArgs) Handles ButtonSubtract.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 2End SubPrivate Sub ButtonMultiply_Click(sender As Object, e As EventArgs) Handles ButtonMultiply.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 3End SubPrivate Sub ButtonDivide_Click(sender As Object, e As EventArgs) Handles ButtonDivide.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 4End SubPrivate Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.ClickTextBoxResult.Text = ""End SubPrivate Sub ButtonEquals_Click(sender As Object, e As EventArgs) Handles ButtonEquals.ClickDim result As DoublesecondNum = Double.Parse(TextBoxResult.Text)Select Case operationCase 1result = firstNum + secondNumCase 2result = firstNum - secondNumCase 3result = firstNum * secondNumCase 4result = firstNum / secondNumEnd SelectTextBoxResult.Text = result.ToStringEnd SubEnd Class```这个计算器包括数字按钮0-9、小数点按钮、加法、减法、乘法、除法和等于按钮。
BASIC程序的语句和编码规则
Byte Boolean Integer Long Single
双精度型 Double
பைடு நூலகம்
类型 符 无 无
% & !
前缀
byt bln int lng sng
#
dbl
所占字节数
1 2 2 4 4
8
货币型 Currency
@
cur
8
日期型 Date(time)
无
dtm
8
字符型 String
$
str 与字符串长度有关
3)浮点数
浮点数也称为实型数或实数,由符号、指数和尾数构成,分单精度 浮点数(Single)和双精度浮点数(Double),其指数部分分别用 “E”(或“e”)和“D”(或“d”)表达。
4)货币型(Currency) 货币型属于定点实数或整数,用8位字节存储小数点前有15位,小 数点后有4位,其他旳数字被舍去,其表达形式为在数字后加上 “@”。
对象型 Objiect
无
obj 4
变体型 Variant
无
vnt 根据分配拟定
范围
0~255 True与False -32768~32767 -2147483648~2147483647 负数:-3.402823E38~-1.401298E-45 正数:1.401298E-45~3.402823E38
5.结束语句
结束语句用来正常地结束一种程序旳执行。结束语句旳格 式为:
End 如下面旳程序:
Private Sub Timer1_Timer() End
End Sub
6.With 语句
With 语句旳语法为: With 对象
[语句块] End With
VB编码
VB编码1.1公式计算Private Sub Command1_Click()Dim a!, b!, c!a = Val(Text1.Text)b = Val(Text2.Text)c = Sqr(a ^ 2 + b ^ 2)Text3.Text = cEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text1.SetFocusEnd Sub1.2个位十位互换Private Sub Form_Load()ShowText1.SetFocusEnd SubPrivate Sub Command1_Click()Dim x%, a%, b%, y%x = Val(Text1.Text)a = x \ 10b = x Mod 10y = b * 10 + aText2.Text = yEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text1.SetFocusEnd Sub1.3产生[50,60]区间的随机数Private Sub Form_Click()Dim a%, b%a = 50b = 60RandomizePrint Int((b - a + 1) * Rnd + a)End Sub1.4产生[10,99]区间内的两个随机数Private Sub Command1_Click()Dim a%, b%, c%, d%a = 10b = 99Randomizec = Int((b - a + 1) * Rnd + a)d = Int((b - a + 1) * Rnd + a)Print "两个随机数:"; c; dPrint "和数:"; c + dEnd Sub2.1个位十位互换Private Sub Command1_Click()Dim p$, a%, b%, x%Clsp = Val(InputBox("请输入一个2位数", "输入框"))a = p \ 10b = p Mod 10x = b * 10 + aPrint "处理后数据:"; xEnd Sub2.2星号图形Private Sub Command1_Click()ClsPrintPrint Tab(6); " * "Print Tab(4); " * * * "Print Tab(2); " * * * * * "End SubPrivate Sub Command2_Click()ClsPrintPrint Tab(6); " * "Print Tab(4); " * * *"Print Tab(2); " * * * * *"Print Tab(4); " * * *"Print Tab(6); " * "End Sub2.3消息演示Private Sub Form_Click()Dim x As Integerx = MsgBox("显示Yes,No,Cancle和警告", 19, "消息演示") Print "x值为:"; xPrintEnd Sub2.4两个输入数据的运算Private Sub Form_Click()Dim x As Integerx = MsgBox("显示Yes,No,Cancle和警告", 19, "消息演示") Print "x值为:"; xPrintEnd Sub3.1找最大值Private Sub Command1_Click()Dim a%, b%, c%, max%a = Val(Text1.Text)b = Val(Text2.Text)c = Val(Text3.Text)If a > b Thenmax = aElsemax = bEnd IfIf c > max Thenmax = cEnd IfText4.Text = maxEnd Sub3.2数据能否同时被2,3,5整除Private Sub Command1_Click()Dim a%Clsa = Val(InputBox("请输入数据", "输入框"))If a Mod 2 = 0 And a Mod 5 = 0 And a Mod 7 = 0 Then Print a; "能被除数2,5,7整除"ElsePrint a; "不能被除数257整除"End IfEnd Sub3.3.1 纳税率()Private Sub Command1_Click()Dim x!x = Val(Text1.Text)If x > 4000 ThenText2.Text = "5%"ElseIf x >= 2001 ThenText2.Text = "3.5%"ElseIf x >= 1000 ThenText2.Text = "2.5%"ElseText2.Text = "0%"End IfEnd IfEnd IfEnd Sub3.3.2纳税率(Select case)Private Sub Command1_Click()Dim x!x = Val(Text1.Text)Select Case xCase Is > 4000Text2.Text = "5%"Case Is >= 2001Text2.Text = "3.5%"Case Is >= 1000Text2.Text = "2.5%"Case ElseText2.Text = "0%"End SelectEnd Sub3.4判断闰年、季节、天数Private Sub Command1_Click()Dim x%, y%, a%x = Val(Text1.Text)y = Val(Text2.Text)If (x Mod 4 = 0 And x Mod 100 <> 0) Or (x Mod 100 = 0) Then Text3.Text = "闰年"Else: Text3.Text = "平年"End IfIf y >= 3 And y <= 5 ThenText4.Text = "春季"ElseIf y >= 6 And y <= 8 ThenText4.Text = "夏季"ElseIf y >= 9 And y <= 11 ThenText4.Text = "秋季"ElseIf y = 1 Or y = 2 Or y = 12 ThenText4.Text = "冬季"Elsea = MsgBox("请输入1-12月份", 21, "提示")Text4.Text = " "Text5.Text = " "End IfEnd IfEnd IfEnd IfIf y = 1 Or y = 3 Or y = 5 Or y = 7 Or y = 8 Or y = 10 Or y = 12 Then Text5.Text = "31天"ElseIf y = 4 Or y = 6 Or y = 9 Or y = 11 ThenText5.Text = "30天"ElseIf (x Mod 4 = 0 And x Mod 100 <> 0) Or (x Mod 100 = 0) ThenText5.Text = "29天"Else: Text5.Text = "28天"End IfEnd IfEnd IfEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text4.Text = ""Text5.Text = ""End Sub5.1求累加和Private Sub Command1_Click()Dim p%, sum%, i%p = InputBox("输入一个自然数", "输入提示")For i = 1 To 10sum = sum + iNext iPrint "1+2+3+4+5+6+7+8+9+10="; sum End Sub5.2求水仙花数Private Sub Command1_Click()Dim i%, a%, b%, c%, n%n = 0Print "水仙花数为:"For i = 100 To 999a = i \ 100b = i \ 10 Mod 10c = i Mod 10If i = a ^ 3 + b ^ 3 + c ^ 3 ThenPrint in = n + 1End IfNext iPrint "水仙花数总个数:"; nEnd Sub5.3求加成5.31Private Sub Form_Click()Dim i%, j%For i = 1 To 5Print Tab(9);For j = 1 To iPrint "*";Next jNext iEnd Sub5.32Private Sub Form_Click()Dim i%, j%For i = 1 To 5Print Tab(10 - i);For j = 1 To 2 * i - 1Print "*";Next jNext iEnd Sub5.33Private Sub Form_Click() Dim i%, j%For i = 1 To 5Print Tab(10 - i);For j = 1 To 2 * i - 1Print "*";Next jNext iEnd Sub。
VB编程程序代码
VB编程程序代码1、求100以内的素Private Sub Form_Click()Dim i%, j%For i = 2 To 100For j = 2 To i - 1If i Mod j = 0 Then Exit For Next jIf j = i Then Print iNext iEnd Sub2、从键盘输入任意长度的字符串,要求将字符顺序倒置例如,将输入的“ABCDEFG”变换成“GFEDCBA”。
Private Sub Command1_Click()Dim a$, I%, c$, d$, n%a = InputBox$("输入字符串")n = Len(a)For I = 1 To Int(n \ 2)c = Mid(a, I, 1)Mid(a, I, 1) = Mid(a, n - I + 1, 1)Mid(a, n - I + 1, 1) = cNext IPrint aEnd Sub3、计算0~200之间所有能被11或5整除的数之和Private Sub Form_Click()Dim n%, i%n = 0For i = 1 To 200If i Mod 11 = 0 Or i Mod 5 = 0 Thenn = n + iEnd IfNext iPrint nEnd Sub4、输入一年份,判断它是否为闰年,并显示有关信息。
(判断闰年的条件是:年份能被4整除但不能被100整除,或者能被400整除)Private Sub Command1_Click()Dim y%y = InputBox("请输入年数")If y Mod 4 = 0 And y Mod 100 <> 0 Or y Mod 400 = 0 Then MsgBox (y & "年是闰年")ElseMsgBox (y & "年是平年")End IfEnd Sub5、已知x,y,z 3个变量中存放了3个不同的数,比较它们的大小并进行调整,使得x<y<z。
vb除法运算
vb除法运算VB是一种广泛应用于Windows平台的高级编程语言,它具有简单易学、功能强大的特点。
在VB中,除法运算是我们经常会用到的运算之一。
本文将以VB除法运算为主题,详细介绍在VB中如何进行除法运算以及注意事项。
在VB中,除法运算使用的符号是“/”,表示除法。
下面我们将通过一些例子来说明。
1. 整数除法在VB中,如果我们对两个整数进行除法运算,结果将会是一个浮点数。
例如,我们对10除以3,代码如下:```Dim num1 As Integer = 10Dim num2 As Integer = 3Dim result As Doubleresult = num1 / num2```在这个例子中,result的值将会是3.3333333333333335。
可以看到,整数除法的结果将会是一个浮点数,而不是一个整数。
这是因为VB 中的除法运算会自动进行类型转换,将整数转换为浮点数,以得到更准确的结果。
2. 浮点数除法除了对整数进行除法运算,我们还可以对浮点数进行除法运算。
例如,我们对2.5除以1.5,代码如下:```Dim num1 As Double = 2.5Dim num2 As Double = 1.5Dim result As Doubleresult = num1 / num2```在这个例子中,result的值将会是1.6666666666666667。
可以看到,浮点数除法的结果也是一个浮点数,而不是一个整数。
3. 零的除法在进行除法运算时,我们需要注意除数不能为零。
如果除数为零,将会导致运行时错误。
例如,我们对一个数除以零,代码如下:```Dim num As Integer = 10Dim zero As Integer = 0Dim result As Doubleresult = num / zero```在这个例子中,由于除数为零,将会导致“除以零”的运行时错误。
为了避免“除以零”的错误,我们可以在进行除法运算之前,先判断除数是否为零。
24VB常用标准函数、基本运算符和表达式
1.7320606 3
说明:e≈2.71828182845905
2.常用的类型转换函数
函数名
函数功能
应用举例
返回值
Asc(X) 字符转换成ASCⅡ码值 Asc(“A”)
65
Chr(X) ASCⅡ码值转换成字符
Chr(65)
“A”
Str(X) 数值转换为字符串
Str(123.45) “123.45”
15.以下VB程序段的功能是统计用户输入的字符串长度。
Private Sub Command1_Click()
Dim a As String a = Text1.Text Label1.Caption = “字符长度为” + ①
End Sub 若要实现该功能,则①处应填写语句为 ____St_r_(_L_e_n(_a_)_)_或__St_r_(_L_e_n(_T_e_x_t_1._T_e_x_t_)_)___。
<=
大于等于
>=
优先级 7 7 7 7 7 7
表达式举例 1=2 1<>2 1<2 1>2
10 Mod 2 <=20 10﹨2>=7
示例表达式的值 False True True False True False
6.逻辑运算符
基本运算 运算符号 优先级
非
Not
8
与
And
9
或
Or
10
表达式举例
Not(2<3) (2<=3) And (2>=3) (2<=3) Or (2>=3)
C.Int(Rnd() * 100)
D.Int(Rnd() * 90) + 10
vb常用程序代码
vb常⽤程序代码1、求半径为r的圆的⾯积,要求⽤户从键盘输⼊半r。
程序代码:Private Sub Form_click()Const PI = 3.14Dim r%Dim s!r = InputBox("请输⼊半径r的值:", "求圆的⾯积") s = PI * r * r Form1.FontSize = 16Print "半径是" + Str(r) + "的圆的⾯积是" + Str(s) End Sub 2、输⼊三个整数,从⼩到⼤顺序输出。
程序代码:Private Sub Form_click()a = InputBox("a")b = InputBox("b")c = InputBox("c")Print "排序前:"; a, b, cIf a > b Then t = a: a = b: b = tIf a > c Then t = a: a = c: c = tIf b > c Then t = b: b = c: c = tPrint "排序后:"; a, b, cEnd Sub3、输⼊三个整数,从⼤到⼩顺序输出。
程序代码:Private Sub Form_click()a = InputBox("a")b = InputBox("b")c = InputBox("c")Print "排序前:"; a, b, cIf aIf a < c Then t = a: a = c: c = tIf b < c Then t = b: b = c: c = tPrint "排序后:"; a, b, cEnd Sub4、输⼊x的值,输出对应的值。
VB计算器加减乘除代码
VB计算器加减乘除代码以下是一个简单的VB计算器程序,可以进行加减乘除运算。
```vbPublic Class Form1Dim num1 As DoubleDim num2 As DoubleDim op As StringPrivate Sub btnNum_Click(sender As Object, e As EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click,btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click,btn9.ClickDim btn As Button = DirectCast(sender, Button)txtDisplay.Text += btn.TextEnd SubPrivate Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.ClicktxtDisplay.Text = ""End SubPrivate Sub btnOperator_Click(sender As Object, e As EventArgs) Handles btnPlus.Click, btnMinus.Click,btnMultiply.Click, btnDivide.ClickDim btn As Button = DirectCast(sender, Button)num1 = Double.Parse(txtDisplay.Text)op = btn.TexttxtDisplay.Text = ""End SubPrivate Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Clicknum2 = Double.Parse(txtDisplay.Text)Dim result As DoubleSelect Case opCase "+"result = num1 + num2Case "-"result = num1 - num2Case "*"result = num1 * num2Case "/"If num2 <> 0 Thenresult = num1 / num2ElseMessageBox.Show("除数不能为0!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)txtDisplay.Text = ""ReturnEnd IfEnd SelecttxtDisplay.Text = result.ToStringEnd SubEnd Class```在这个计算器程序中,我使用了一个文本框(txtDisplay)来显示用户输入的数字和运算结果。
vb除法运算代码
vb除法运算代码VB语言中,除法运算可以使用除法运算符“/”或使用除法函数来实现。
除法运算是指将一个数除以另一个数,得到的商为结果的一种基本数学运算。
使用除法运算符“/”进行除法运算的语法如下所示:```result = number1 / number2```其中,number1和number2是要进行除法运算的两个数,result 是存储运算结果的变量。
除法运算所得的结果是浮点数,即使number1能够整除number2,结果也会包含小数部分。
如果想要获得整数结果,可以使用整除运算符“\”。
除法函数在VB语言中有两种常用形式:CDbl和CInt。
CDbl 函数将参数转换为双精度浮点数,而CInt函数将参数转换为整数。
使用CDbl函数进行除法运算的语法如下所示:```result = CDbl(number1) / CDbl(number2)```使用CInt函数进行除法运算的语法如下所示:```result = CInt(number1) / CInt(number2)```除法运算时需要注意以下几点:1. 如果除数为0,会导致运行时错误“除以零错误”(Divide by zero error)。
在进行除法运算前,可以使用条件语句判断除数是否为0,以避免出现错误。
2. VB语言中的除法运算是按照IEEE 754标准进行的,即如果其中一个操作数为浮点数,那么结果也会是浮点数。
以下是一个完整的VB代码示例,展示了使用除法运算符进行除法运算的例子:```vbPublic Sub DivisionExample()Dim number1 As DoubleDim number2 As DoubleDim result As Doublenumber1 = 10number2 = 3If number2 <> 0 Thenresult = number1 / number2MsgBox "The result of division is: " & resultElseMsgBox "Cannot divide by zero."End IfEnd Sub```以上是有关VB语言中除法运算的相关参考内容。
VB程序编码
1、创建一个窗体,在窗体上添加两个文本框和一个命令按钮,标题为“交换”,程序运行时用户在两个文本框中本别输入数据,然后点击交换按钮,交换两个文本框中的数据Private sub command1_click()C=text1.textText1.text=text2.textText2.text=cEnd sub2、1、创建一个窗体,输入学号、姓名、年龄、籍贯;然后通过标签显示用户输入的信息。
通过此程序掌握调试技巧。
如下图所示:Private sub command1_click()Label5.caption=label1.caption&”:”&text1.text&_Label2.caption&”:”text2.text&_Label3,caption&”:”&text3.text&_End sub3、输入二次方程的三个系数,然后计算二次方程的根。
平方根函数为sqr()Dim a as integerDim b as integerDim c as integera = val (text1.text)b = val (text1.text)c = val (text1.text)dim x1 as singeldim x2 as singelx1 = (-b + sqr(b*b-4*a*c))/(2*a)x2 = (-b -sqr(b*b-4*a*c))/(2*a)label5.caption=”x1=” & x1 & vbcrlf & ”x2=”& x24、输入三角形边三个边长,计算三角形面积。
面积公式:△=,其中s=(a+b+c)/2Dim a as integerDim b as integerDim c as integera = val (text1.text)b = val (text1.text)c = val (text1.text)s = (a+b+c)/2area = sqr(s*(s-a)*(s-b)*(s-c))label5.caption = “面积:“&areas(s-a)(s-b)(s-c)End up方法二Dim a as integerDim b as integerDim c as integera = val (text1.text)b = val (text1.text)c = val (text1.text)if a +b < c or a+c< b or b+c<o then print “不成立”s = (a+b+c)/2area = sqr(s*(s-a)*(s-b)*(s-c))else print areas(s-a)(s-b)(s-c)5、输入三个数,由小到大输出Dim a as Singela= val(text1.text)b= val(text2.text)c= val(text3.text)If a <b thent = a :a = b :b =tend ifif a < c thent = a :a = c : c=tend ifif b <c thent = b:b=c:c=tprint a ; b;cend sub6,输入一个数,判断奇偶性Private sub command1_click()Dim a as integerA = val (text1.text )If a mod 2= 0 then label 1.caption = “偶数”Else label1.caption =”奇数”方法二Private sub command1_click()Dim a as integerA = val (text1.text )If a mod 2= 0 thenS = “偶数”Elses= “奇数“end iflabel1.capion=send sub6, 求解一元二次方程Dim a as integer Dim b as integer Dim c as integera = val (text1.text)b = val (text1.text)c = val (text1.text)d = b ^2 – 4*a*cif a <>0 thenif d > 0 thenx1 = (-b)/(2*a)label1.capyion=”x1”&x1”:x2”&x2end ifelselabel1.capyion = “无实根”end ifelseend ifend sub6输入百分制成绩,要求输出成绩等级'A','B','C','D','E'。
VB小学数学四则运算程序代码
Option Base 1 '标识数组下标从1开始Dim x, y, z As IntegerPrivate Sub Command1_Click()Dim a(4) As StringDim b As IntegerText1.Text = Int(10 * Rnd + 0) '文本框1中随机产生0 - 9中的一个数a(1) = "+"a(2) = "-"a(3) = "*"a(4) = "/"b = Int(4 * Rnd + 1)Label1.Caption = a(b) '标签1随机产生“加”、“减”、“乘”、“除”号If Label1.Caption = a(4) ThenText2.Text = Int(9 * Rnd + 1) '如果标签1出现的是“/”号,则文本框2随机产生1-9中的一个数ElseText2.Text = Int(10 * Rnd + 0) '如果不是则文本框2随机产生0-9中的一个数End IfText3.SetFocusEnd SubPrivate Sub Command2_Click()Dim c As SingleIf IsNumeric(Text3.Text) = False ThenMsgBox "请输入数字!", vbOKOnly + vbExclamation, "提示" '如果文本框3输入的不是数字或没有输入,则提示“请输入数字!”Text3.Text = ""Text3.SetFocusElseSelect Case Label1.CaptionCase "+"c = Val(Text1.Text) + Val(Text2.Text)Case "-"c = Val(Text1.Text) - Val(Text2.Text)Case "*"c = Val(Text1.Text) * Val(Text2.Text)Case "/"c = Val(Text1.Text) / Val(Text2.Text) End SelectIf Text3.Text = c ThenMsgBox "恭喜你,回答正确!", vbOKOnly + vbInformation, "提示"x = x + 1Text4.Text = xz = z + 10Text6.Text = zElseMsgBox "回答错误!" & " " & "正确结果为" & c, vbOKOnly + vbCritical, "提示"y = y + 1Text5.Text = yz = z - 10Text6.Text = zEnd IfEnd IfEnd SubPrivate Sub Form_Load() RandomizeEnd SubPrivate Sub Label1_Change() Text3.Text = ""End SubPrivate Sub Text1_Change() Text3.Text = ""End SubPrivate Sub Text2_Change() Text3.Text = ""End Sub。
用VB制作四则运算器步骤及代码
⽤VB制作四则运算器步骤及代码⽤V B制作四则运算器步骤及代码Company Document number:WUUT-WUUY-WBBGB-BWYTT-1982GT⼀、实验⽬的、⽬标综合运⽤本课程的相关知识,完成两位数四则运算器的设计、编写与测试,培养学⽣的实践动⼿能⼒,并检测本课程的学习效果。
⼆、实验内容基本要求:两位数(指10-99的⾃然数)的加减乘除(除法指的是整除)运算,其中减法和除法必须满⾜“被操作数不⼩于操作数”(即可以不考虑20-56、4\20等情况)两个操作数、运算符均随机产⽣完成⼀题后⾃动判断对错,并显⽰出来可以随时查看已经完成的题⽬情况完成⼀题⾃动计算题⽬总数、正确数、错误数和正确率,并实时显⽰出来扩展要求(不硬性规定):在答错的题⽬后⾯给出正确答案可增加时钟控件,10秒(或15秒)内必须完成⼀题限制题⽬数⽬为固定值(如20题、50题)把程序扩展成为多位数甚⾄任意位数的运算器三、实验过程与步骤1、在Visual Basic 系统环境下,打开“⼯程设计”窗⼝。
2、新建两个窗⼝Form1和Form2,添加控件并进⾏控件的设置, Form1作⽤是进⾏题⽬数量和答题时间的选择并将数据进⾏保存以供Form2调⽤,Form2的作⽤是进⾏随机两位数(指10-99的⾃然数)的加减乘除(除法指的是整除)运算,完成⼀题后⾃动判断对错,给出错误题⽬的正确答案,并显⽰出来,添加时间钟控,显⽰计算题⽬总数、正确数、错误数和正确率。
3、在Form1窗⼝,双击控件打开代码窗⼝,设计窗体及控件的事件代码。
采⽤单选的形式选择题⽬数量和答题时间,当点击确定按钮时,调⽤Form2,当点击退出时,退出整个程序。
Public tishu As Integer, shijian As IntegerPrivate Sub Form_Load()= False= False==MsgBox "请选择题量和答题时间!", 48, "提⽰"Unload Form2End SubPrivate Sub Option1_Click(Index As Integer)tishu = Val(Option1(Index).Caption)= tishuEnd SubPrivate Sub Option2_Click(Index As Integer)shijian = Val(Option2(Index).Caption)= shijianEnd SubPrivate Sub Command1_Click()MsgBox "请直接在⽂本框中输⼊数字作答(其中除法为整除)," & vbLf & "回车完成⼀题,系统⾃动⽣成下⼀题", , "提⽰" End SubPrivate Sub Command2_Click()EndEnd Sub4、在Form2窗⼝,双击控件打开代码窗⼝,设计窗体及控件的事件代码。
vb编程代码大全
vb编程代码大全VB(Visual Basic)是一种易于学习和使用的编程语言,广泛应用于Windows平台的软件开发。
VB编程通过编写和执行代码来实现任务,可以用于创建各种类型的应用程序,包括窗体应用程序、控制台应用程序、Web应用程序等。
本文将介绍一些常用的VB编程代码,帮助读者快速入门和掌握VB编程技巧。
一、VB基本语法在开始编写VB代码之前,我们先来了解一些基本的VB语法规则。
1. 注释VB中的注释用于解释代码的作用,不会被编译器执行。
注释可以是单行注释(以“'”开头)或多行注释(以“/*”开始,“*/”结束)。
示例:' 这是一个单行注释/*这是一个多行注释可以跨越多行*/2. 变量声明在VB中,变量需要先声明后使用。
变量的声明可以包括数据类型和变量名称。
示例:Dim x As Integer '声明一个整型变量x3. 变量赋值在VB中,可以使用赋值语句将一个值赋给变量。
示例:x = 10 '将变量x赋值为104. 运算符VB支持常见的运算符,包括算术运算符、比较运算符和逻辑运算符等。
示例:Dim a As Integer, b As Integera = 10b = 5Dim c As Integerc = a + b '加法运算c = a - b '减法运算c = a * b '乘法运算c = a / b '除法运算c = a Mod b '取余运算c = a > b '比较运算c = (a > 0) And (b > 0) '逻辑运算5. 控制结构VB中的控制结构包括条件语句和循环语句。
条件语句通过判断条件来选择不同的执行路径。
循环语句可以重复执行一段代码。
示例:If condition Then' 执行语句块1ElseIf condition2 Then' 执行语句块2Else' 执行语句块3End IfFor i = 1 To 10' 执行循环体Next i以上是VB的基本语法规则,通过掌握这些语法,可以编写简单的VB程序。
VB简单计算代码
VB简单计算代码一、设置控件属性text1(被除数):text=(空)label1:caption=“/”text2(除数):text=(空)label2:caption=“=”text3(商):text=(空)command1:caption=“计算”command2:caption=“清空”二、编写代码Private Sub Command1_Click()Dim a As Single,b As Single,c As Single (single单精度,可以精确到十进制的7位,)a=Val(Text1.Text)b=Val(Text2.Text)c=a/bText3.Text=cEnd SubPrivate Sub Command2_Click()Text1.Text=""Text2.Text=""Text3.Text=""End Sub1. 算术运算符VB中有八种运算符,按优先级别的高低顺序分别是:(1)指数运算符:^(2)取负:-(3)乘法*、除法/(4)整除:\(5)取模(取余):Mod(6)加+、减-在书写VB算术表达式时应注意:(1)可以用圆括弧()改变表达式的运算顺序,括弧内的运算符总是先被运算。
(2)同级别的运算符在一起时,按自左向右顺序运算。
(3)指数运算符与取负运算符在一起时,先取负。
比如3^-2表示3的-2次方。
(4)为了提高表达式的可读性,应尽可能用“()”标识出运算顺序。
2. 关系运算符关系运算符主要用于对两个运算量进行关系比较。
所谓运算量可以是常量、变量或算术表达式、函数等。
通过关系运算符将两个运算量连接起来形成关系表达式,关系表达式的值是逻辑值,只有真True 和假False两个值。
在相应的场合,VB把0解释为False,而任何非0的解释为True。
VB有六种关系运算符:大于 (>)小于 (<)等于 (=)不等于(<>)大于等于 (>=)小于等于(<=)它们的优先级是相同的,按自左向右运算,即用于对字符串或数字型数据进行比较。
vb源程序代码
Vb源代码(周至县第五中学710402 数学组唐永鸽)1 Private Sub Form_Click()Dim n As Integer, f1 As Double, f2 As Double, f3 As DoubleDim i As Integern = Val(InputBox("输入要输出的项数:", "输出斐波那契数列", 20)) f1 = 0f2 = 1Print Tab(8); f1Print Tab(8); f2For i = 3 To nf3 = f2 + f1Print Tab(8); f3f1 = f2f2 = f3NextEnd Sub2 Private Sub Form_Click()Dim n As Integer, f1 As Doublen = 1For n = 1 To 66f1 = 15 * nPrint f1NextEnd Sub3 Private Sub Form_Click()Dim a1 As Double, n As Double, i As Integera1 = V al(InputBox("请输入第1个数", "找最大数"))Print Tab(5); a1For i = 2 To 5n = Val(InputBox("请输入第" & i & "个数"))Print Tab(5); nIf n > a1 Then a1 = nNextPrint ""Print "最大数为"; a1End Sub4、Private Sub Form_Click()Dim year As Longyear = InputBox("请输入年份")If year Mod 4 = 0 ThenIf year Mod 100 = 0 ThenIf year Mod 400 = 0 ThenPrint year; "年是闰年"ElsePrint year; "不年是闰年"End IfElsePrint year; "年是闰年"End IfElsePrint year; "不年是闰年"End IfEnd Sub5、Private Sub form_Click() Dim year As LongFor year = 2000 To 2500If year Mod 4 = 0 ThenIf year Mod 100 = 0 ThenIf year Mod 400 = 0 ThenPrint year; "年是闰年", ElsePrint year; "年不是闰年", End IfElsePrint year; "年是闰年", End IfElsePrint year; "年不是闰年",End IfNextEnd Sub6、Option Base 1Private Sub Form_Click()Const n = 10Dim a(n) As SingleMax = 0k = 0For i = 1 To na(i) = Val(InputBox("请输入第" & i & "个数", "找最大数"))Print Tab(5); "第" & i & "个数为:"; a(i)Next iFor i = 1 To nIf Max < a(i) ThenMax = a(i)k = iEnd IfNextPrintPrint "最大数为第" & k & "个数"; MaxEnd Sub7 、方法1、 Private Sub Form_Click()Dim n As Integer, f1 As Integer, f2 As Integer, f3 As Integer For n = 100 To 999f1 = Int(n / 100)f2 = Int(n / 10) Mod 10f3 = n Mod 10If f1 ^ 3 + f2 ^ 3 + f3 ^ 3 = n ThenPrint n; "为水仙花数"End IfNextEnd Sub方法2、Private Sub Form_Click()Dim n As Integer, f1 As Integer, f2 As Integer, f3 As Integer n = 100Do While n <= 999f1 = Int(n / 100)f2 = Int(n / 10) Mod 10f3 = n Mod 10If f1 ^ 3 + f2 ^ 3 + f3 ^ 3 = n ThenPrint n; "为水仙花数"End Ifn = n + 1LoopEnd Sub8 、 Private Sub Form_Click()Dim n As Integer, s As Integern = 1s = 0Dos = s + nn = n + 1Loop While s <= 200n = n - 2Print nEnd Sub9、二分法方法1、Public Function f(x As Double) As Doublef = x ^ 3 - x - 1End FunctionPrivate Sub command1_Click()Dim a As Double, b As Double, p As Singlea = 1b = 1.5p = 0.00001DoIf f((a + b) / 2) = 0 ThenExit DoElseIf f(a) * f((a + b) / 2) < 0 Thenb = (a + b) / 2Elsea = (a + b) / 2End IfEnd IfLoop While b - a >= pLabel1.Caption = "方程的根是" & (a + b) / 2End SubPrivate Sub Command2_Click()Label2.Caption = "求方程 f = x ^ 3 - x - 1的近似解" End Sub方法2、Private Sub Command1_Click()a = Val(Text1.Text)b = Val(Text2.Text)e = Val(Text3.Text)fa = (a * a - 1) * a - 1fb = (b * b - 1) * b - 1If fa * fb >= 0 ThenText4.Text = "求解范围错!"ElseDox = (a + b) / 2f = (x * x - 1) * x - 1If f * fa > 0 Thena = x: fa = fElseb = x: fb = fEnd IfLoop Until f = 0 Or Abs(b - a) < eText4.Text = (a + b) / 2End IfEnd Sub10、冒泡排序Private Sub Command1_Click()Dim C(6) As DoubleFor n = 1 To 6C(n) = InputBox("输入需要排序的数", "6个数的冒泡排序", "") Next nFor i = 1 To 6For j = 6 To i Step -1If C(j) < C(j - 1) Thena = C(j): C(j) = C(j - 1): C(j - 1) = aEnd IfNext jNext iFor i = 1 To 6Print "", C(i)Next iEnd Sub11、求12+22+32+…+1002的算法Private Sub Form_Load()Dim n As Long, s As Longn = InputBox("请输入 n:", "从1到n求平方和", 100)s = 0i = 1While i <= ns = s + i * ii = i + 1WendForm1.ShowPrint "", "1 到 "; n; " 的平方和是:"; sEnd Sub12、辗转相除法求最大公因数Private Sub Form_Click()a = InputBox("请输入一个整数")b = InputBox("请输入一个整数")n1 = Abs(a)n2 = Abs(b)Do While n1 Mod n2 <> 0tt = n1 Mod n2n1 = n2n2 = ttLoopLabel1.Caption = a & "和" & b & "的最大公因数为" & n2End Sub13、用秦九韶算法求多项式的值Private Sub Command1_Click()Dim C(5) As Doublex = InputBox("请输入求值数", "")C(0) = -0.8: C(1) = 1.7: C(2) = -2.6: C(3) = 3.5: C(4) = 2: C(5) = 5 n = 1v = C(5)While n <= 5v = v * x + C(5 - n)n = n + 1WendPrint "所求的值是"; vEnd Sub14 .判断素数Private Sub Form_Click()Dim n As LongDim i As LongDim r As Singlen = Val(InputBox("请输入一个数", "判断素数"))For i = 2 To n - 1r = n Mod iIf r = 0 ThenPrintPrint n; "不是素数"GoTo 17End IfNextPrintPrint n; "为素数"17 End Sub15Private Sub Form_Click()Dim i As Integer, sum As Doublesum = 1For i = 1 To 64sum = sum + 2 ^ iNext iPrint sumEnd Sub16、猜字游戏Private Sub Command1_Click()RandomizeDim Num As LongDim RightAnswer As BooleanDim InputNum As LongText1.Text = ""Num = Int(Rnd * 10) + 1RightAnswer = FalseText1.Text = Text1.Text + "Here's a number between 1 and 10. Can you guess it?" + vbNewLineWhile RightAnswer = FalseInputNum = InputBox("Enter a number")If InputNum = Num ThenText1.Text = Text1.Text & InputNum & " " & "You guessed it!" & vbNewLineRightAnswer = TrueElseIf InputNum < Num ThenText1.Text = Text1.Text & InputNum & " " & "Too Small" & vbNewLineElseIf InputNum > Num ThenText1.Text = Text1.Text & InputNum & " " & "Too Large" & vbNewLineEnd IfWendEnd Sub17、石头、剪刀、布Private Sub Form_Activate()Option1.Caption = "石头"Option2.Caption = "剪刀"Option3.Caption = "布"Option1.Value = FalseOption2.Value = FalseOption3.Value = FalseEnd SubPrivate Sub Option1_Click()RandomizeSelect Case Int(3 * Rnd)Case 0: MsgBox "对方也出石头!继续!"Case 1: MsgBox "哈哈!你赢了!对方出的是剪刀!奖励你一个苹果!"Case 2: MsgBox "你输了!对方出的是布哦!不好意思,苹果给对方了哈!" End SelectOption1.Value = FalseEnd SubPrivate Sub Option2_Click()RandomizeSelect Case Int(3 * Rnd)Case 0: MsgBox "你输了!对方出的是石头哦!不好意思,苹果给对方了哈!" Case 1: MsgBox "对方也出剪刀!继续!"Case 2: MsgBox "哈哈!你赢了!对方出的是布!奖励你一个苹果!"End SelectOption2.Value = FalseEnd SubPrivate Sub Option3_Click()RandomizeSelect Case Int(3 * Rnd)Case 0: MsgBox "哈哈!你赢了!对方出的是石头!奖励你一个苹果!"Case 1: MsgBox "你输了!对方出的是剪刀哦!不好意思,苹果给对方了哈!" Case 2: MsgBox "对方也出布!继续!"End SelectOption3.Value = FalseEnd Sub18、选择题计分程序Private Sub Command1_Click() '提交按钮CommandButton1Dim sum As Integersum = 0If Text1.Text = "A" Thensum = sum + 5Image1.Picture = LoadPicture("E:\课件素材\图片\对号.jpg")ElseImage1.Picture = LoadPicture("E:\课件素材\图片\错号.jpg")End IfIf Text2.Text = "B" Thensum = sum + 5Image2.Picture = LoadPicture("E:\课件素材\图片\对号 - 副本.jpg") ElseImage2.Picture = LoadPicture("E:\课件素材\图片\错号 - 副本.jpg") End IfText3.Text = "您的最后得分是:" & sum & "分" & vbNewLineIf sum <= 5 ThenText3.Text = Text3.Text + "加油啊!"ElseText3.Text = Text3.Text + "恭喜您!及格了!"End IfEnd SubPrivate Sub Command2_Click()Text1.Text = ""Image1.Picture = LoadPicture()Text2.Text = ""Image2.Picture = LoadPicture()Text3.Text = ""End Sub18、韩信点兵Private Sub Command1_Click()Dim X As Integer, Y As IntegerX = 1: Y = 0DoIf X Mod 3 = 1 ThenIf X Mod 5 = 2 ThenIf X Mod 7 = 4 ThenIf X Mod 13 = 6 ThenIf X Mod 17 = 8 Then Y = X End IfEnd IfEnd IfEnd IfX = X + 1Loop Until Y > 0Print "韩信统御士兵数:"; YEnd SubPrivate Sub Form_Click()k = 0s = 0100k = k + 1s = s + kIf k < 100 Then GoTo 100Print sEnd Sub。
除法运算(VB)
lblResult.Caption = lblResult.Caption & s
txtString.Text = "" End Sub
字符串连接符
(4)运行程序验证
输入you后的效果
3个字符串的连接结果
2.1.2 系统函数的使用方法
【实例2.4】窗体中有2个标签、1个文本框和1个命 令按钮。程序运行时,在文本框中输入一个字符, 单击“显示Ascii码值”命令按钮,在标签中显示 出相应的ASCII码值。
(置空)
对象 文本框2
文本框3 命令按钮1 命令按钮2
属性 (名称) MaxLength
Text (名称) MaxLength
Text (名称) Caption (名称) Caption
属性值
txtMiddle 20
(置空)
txtDown 20
(置空)
cmdToDown 上到下
cmdToUp 下到上
(2)设置各对象的属性
对象 窗体 命令按钮
属性
(名称) Caption
(名称) Caption
属性值
frmEx2_5 变色窗体
cmdBkColor 背景色
(3)编写单击“背景色”命令按钮的代码
Private Sub cmdBkColor_Click()
Dim colnum As Integer Randomize 保证与前一次的产生值无关
v 4 r 3
3
s 4r2
操作步骤
(1)设计用户界面
添加5个标签、1个命令按钮和1个文本框
(2)设置各对象的属性
对象 窗体 标签1 标签2 标签3
标签4
标签5