VB如何使用减法

VB如何使用减法,第1张

如果你的对象名没错的话,应该是可以减掉的,没减掉的原因可能是几个文本框的对象名错了,你好好查一下每一个文本框的属性中name属性是不是分别为text1/text2/text3,而且顺序也没错?

Option Explicit

Dim Op1, Op2 Dim DecimalFlag As Integer Dim NumOps As Integer Dim LastInput

Dim OpFlag Dim TempReadout

Private Sub Cancel_Click()

Readout = Format(0, "0")

Op1 = 0

Op2 = 0

Form_Load

End Sub

Private Sub CancelEntry_Click()

Readout = Format(0, "0")

DecimalFlag = False

LastInput = "CE"

End Sub

Private Sub Decimal_Click()

If LastInput = "NEG" Then

Readout = Format(0, "-0")

ElseIf LastInput <> "NUMS" Then

Readout = Format(0, "0")

End If

DecimalFlag = True

LastInput = "NUMS"

End Sub

Private Sub Form_Load()

DecimalFlag = False

NumOps = 0

LastInput = "NONE"

OpFlag = " "

Readout = Format(0, "0")

'DecimalCaption = Format(0, "")

End Sub

Private Sub Number_Click(Index As Integer)

If LastInput <> "NUMS" Then

Readout = Format(0, "")

DecimalFlag = False

End If

If DecimalFlag Then

Readout = Readout + Number(Index)Caption

Else

Readout = Left(Readout, InStr(Readout, Format(0, "")) - 1) + Number(Index)Caption + Format(0, "")

End If

If LastInput = "NEG" Then Readout = "-" & Readout

LastInput = "NUMS"

End Sub

Private Sub Operator_Click(Index As Integer)

TempReadout = Readout

If LastInput = "NUMS" Then

NumOps = NumOps + 1

End If

Select Case NumOps

Case 0

If Operator(Index)Caption = "-" And LastInput <> "NEG" Then

Readout = "-" & Readout

LastInput = "NEG"

End If

Case 1

Op1 = Readout

If Operator(Index)Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then

Readout = "-"

LastInput = "NEG"

End If

Case 2

Op2 = TempReadout

Select Case OpFlag

Case "+"

Op1 = CDbl(Op1) + CDbl(Op2)

Case "-"

Op1 = CDbl(Op1) - CDbl(Op2)

Case "X"

Op1 = CDbl(Op1) CDbl(Op2)

Case "/"

If Op2 = 0 Then

MsgBox "除数不能为 0", 48, "计算器"

Else

Op1 = CDbl(Op1) / CDbl(Op2)

End If

Case "="

Op1 = CDbl(Op2)

Case "%"

Op1 = CDbl(Op1) CDbl(Op2)

End Select

Readout = Op1

NumOps = 1

End Select

If LastInput <> "NEG" Then

LastInput = "OPS"

OpFlag = Operator(Index)Caption

End If

End Sub

Private Sub Percent_Click()

Readout = Readout / 100

LastInput = "Ops"

OpFlag = "%"

NumOps = NumOps + 1

DecimalFlag = True

End Sub 窗体代码 VERSION 500

Begin VBForm Calculator

BorderStyle = 1 'Fixed Single

Caption = "计算器"

ClientHeight = 2970

ClientLeft = 2580

ClientTop = 1485

ClientWidth = 3270

ClipControls = 0 'False

BeginProperty Font

Name = "System"

Size = 975

Charset = 0

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Icon = "CALCfrx":0000

LinkMode = 1 'Source

LinkTopic = "Form1"

MaxButton = 0 'False

PaletteMode = 1 'UseZOrder

ScaleHeight = 2970

ScaleWidth = 3270

WhatsThisHelp = -1 'True

Begin VBCommandButton Number

Caption = "7"

Height = 480

Index = 7

Left = 120

TabIndex = 7

Top = 600

Width = 480

End

Begin VBCommandButton Number

Caption = "8"

Height = 480

Index = 8

Left = 720

TabIndex = 8

Top = 600

Width = 480

End

Begin VBCommandButton Number

Caption = "9"

Height = 480

Index = 9

Left = 1320

TabIndex = 9

Top = 600

Width = 480

End

Begin VBCommandButton Cancel

Caption = "C"

Height = 480

Left = 2040

TabIndex = 10

Top = 600

Width = 480

End

Begin VBCommandButton CancelEntry

Caption = "CE"

Height = 480

Left = 2640

TabIndex = 11

Top = 600

Width = 480

End

Begin VBCommandButton Number

Caption = "4"

Height = 480

Index = 4

Left = 120

TabIndex = 4

Top = 1200

Width = 480

End

Begin VBCommandButton Number

Caption = "5"

Height = 480

Index = 5

Left = 720

TabIndex = 5

Top = 1200

Width = 480

End

Begin VBCommandButton Number

Caption = "6"

Height = 480

Index = 6

Left = 1320

TabIndex = 6

Top = 1200

Width = 480

End

Begin VBCommandButton Operator

Caption = "+"

Height = 480

Index = 1

Left = 2040

TabIndex = 12

Top = 1200

Width = 480

End

Begin VBCommandButton Operator

Caption = "-"

Height = 480

Index = 3

Left = 2640

TabIndex = 13

Top = 1200

Width = 480

End

Begin VBCommandButton Number

Caption = "1"

Height = 480

Index = 1

Left = 120

TabIndex = 1

Top = 1800

Width = 480

End

Begin VBCommandButton Number

Caption = "2"

Height = 480

Index = 2

Left = 720

TabIndex = 2

Top = 1800

Width = 480

End

Begin VBCommandButton Number

Caption = "3"

Height = 480

Index = 3

Left = 1320

TabIndex = 3

Top = 1800

Width = 480

End

Begin VBCommandButton Operator

Caption = "X"

Height = 480

Index = 2

Left = 2040

TabIndex = 14

Top = 1800

Width = 480

End

Begin VBCommandButton Operator

Caption = "/"

Height = 480

Index = 0

Left = 2640

TabIndex = 15

Top = 1800

Width = 480

End

Begin VBCommandButton Number

Caption = "0"

Height = 480

Index = 0

Left = 120

TabIndex = 0

Top = 2400

Width = 1080

End

Begin VBCommandButton Decimal

Caption = ""

Height = 480

Left = 1320

TabIndex = 18

Top = 2400

Width = 480

End

Begin VBCommandButton Operator

Caption = "="

Height = 480

Index = 4

Left = 2040

TabIndex = 16

Top = 2400

Width = 480

End

Begin VBCommandButton Percent

Caption = "%"

Height = 480

Left = 2640

TabIndex = 17

Top = 2400

Width = 480

End

Begin VBLabel Readout

Alignment = 1 'Right Justify

BackColor = &H0000FFFF&

BorderStyle = 1 'Fixed Single

Caption = "0"

BeginProperty Font

Name = "MS Sans Serif"

Size = 12

Charset = 0

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

ForeColor = &H00000000&

Height = 375

Left = 120

TabIndex = 19

Top = 105

Width = 3000

End

End

Attribute VB_Name = "Calculator"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

在窗体上放2个命令按纽,3个文本框,2个标签,如图,代码如下

Option Explicit: Dim a As Double, b As Double

Private Sub Command1_Click()

a = Val(Text1Text)

b = Val(Text2Text)

If Text1Text <> "" And Text2Text <> "" Then

Text3Text = a + b

End If

Label1Caption = "+"

Label2Caption = "="

End Sub

Private Sub Command2_Click()

a = Val(Text1Text)

b = Val(Text2Text)

If Text1Text <> "" And Text2Text <> "" Then

Text3Text = a - b

End If

Label1Caption = "-"

Label2Caption = "="

End Sub

Private Sub Form_Load()

Dim i As Integer

For i = 1 To 3

Controls("text" & i)Text = ""

Next i

Command1Caption = "加法"

Command2Caption = "减法"

Label1Caption = ""

Label2Caption = ""

End Sub

Dim BDS, JSS, I As Integer

Private Sub Command1_Click()

Dim kkl() As String   '取出各行表达式的运算符的数值

ReDim kkl(UBound(BDS))

For I = 0 To UBound(BDS)

kkl(I) = Right(Left(BDS(I), 3), 1)   '取出表达式的运算符

Select Case kkl(I)

  Case "+"

    JSS = Split(BDS(I), "+")   '取出运算符二边的数据,存储在 JSS 数组中,下同

    Text2Text = Text2Text & Val(JSS(0)) + Val(JSS(1)) & vbCrLf    '在文本框2中显示计算结果。数据必须转换为数字数据!,下同

  Case "-"

    JSS = Split(BDS(I), "-")

    Text2Text = Text2Text & Val(JSS(0)) - Val(JSS(1)) & vbCrLf

  Case ""

    JSS = Split(BDS(I), "")

    Text2Text = Text2Text & Val(JSS(0))  Val(JSS(1)) & vbCrLf

  Case "/"

    JSS = Split(BDS(I), "/")

    Text2Text = Text2Text & Val(JSS(0)) / Val(JSS(1)) & vbCrLf

End Select

Next I

End Sub

Private Sub Form_Load()

Text1Text = Text1Text & "3333" & vbCrLf

Text1Text = Text1Text & "33+33" & vbCrLf

Text1Text = Text1Text & "3333" & vbCrLf

BDS = Split(Text1Text, vbCrLf) '将数据存储到 BDS 数组中

End Sub

这个程序符合二位数与二位数的+、-、、/ 的计算,比较简单。

减法就和平常的减法计算是一样的,也是用"-"这个符号运算把下面代码复制到窗体里直接运行即可Private

Sub

Form_Load()

Dim

a

As

Single

Dim

b

As

Single

Dim

c

As

Single

a

=

10

'定义a为10

b

=

55

'定义b为55c

=

a

-

b

'计算a-b

MsgBox

"a-b=10-55="

&

c

'显示结果

End

Sub

代码如下 \x0d\'已增加 *** 作数正确性验证及 除数不能为0验证 \x0d\\x0d\Private Sub Command1_Click() \x0d\If IsNumeric(Text1Text) And Text1Text "" And IsNumeric(Text2Text) And Text2Text "" Then \x0d\Select Case Combo1ListIndex \x0d\Case 0 \x0d\Text3Text = Val(Text1Text) + Val(Text2Text) \x0d\Case 1 \x0d\Text3Text = Val(Text1Text) - Val(Text2Text) \x0d\Case 2 \x0d\Text3Text = Val(Text1Text) Val(Text2Text) \x0d\Case 3 \x0d\If Val(Text1Text) 0 Then \x0d\Text3Text = Val(Text1Text) / Val(Text2Text) \x0d\Else \x0d\MsgBox "除数不能为0", , "错误" \x0d\End If \x0d\End Select \x0d\Else \x0d\MsgBox "请输入 *** 作数", , "错误" \x0d\End If \x0d\End Sub \x0d\\x0d\Private Sub Form_Load() \x0d\Combo1AddItem "+" \x0d\Combo1AddItem "-" \x0d\Combo1AddItem "" \x0d\Combo1AddItem "/" \x0d\Combo1ListIndex = 0 \x0d\Text1Text = "" \x0d\Text2Text = "" \x0d\Text3Text = "" \x0d\Command1Caption = "计算" \x0d\\x0d\End Sub

Private Sub Command1_Click()

Randomize

Dim a(10) As Integer, b(10) As Integer

Dim c As Integer

For i = 0 To 9

a(i) = Int(Rnd 10)

b(i) = Int(Rnd 10)

c = Int(Rnd 2)

If c = 0 Then

Print a(i) & " + " & b(i) & " = " & a(i) + b(i)

Else

Print a(i) & " - " & b(i) & " = " & a(i) - b(i)

End If

Next

End Sub

以上就是关于VB如何使用减法全部的内容,包括:VB如何使用减法、如何用VB编写一个加减乘除计算器只要代码!、我想要一个用VB6.0编的加减法程序,点了控件“加法”“减法”才可以在第三个文本框中出现结果的程序代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10208442.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存