Public Class Triangle
Inherits Form1
Private SideA As Single
Property a() As Single
Get
Return SideA
End Get
Set(value As Single)
SideA = value
End Set
End Property
Private SideB As Single
Property b() As Single
Get
Return SideA
End Get
Set(value As Single)
SideB = value
End Set
End Property
Private SideC As Single
Property C() As Single
Get
Return SideA
End Get
Set(value As Single)
SideC = value
End Set
End Property
Private Semiperimeter As Single
ReadOnly Property s() As Single
Get
Return Semiperimeter
End Get
End Property
'consrtuctor
Sub New(ByVal sngSideA As Single, ByVal sngSideB As Single, ByVal sngSideC As Single)
Me.SideA = sngSideA
Me.SideB = sngSideB
Me.SideC = sngSideC
Me.Semiperimeter = 0.5 * (SideA + SideB + SideC)
End Sub
'Public methods
'-----------------------------------------------------------------------
'Returns true if sides are a,b,c are valid for a triangle
'post:true returned if te sum of sides a and b are greaerthan side c. False returned otherwise
'-----------------------------------------------------------------------
Public Function ValidTriangle() As Boolean
If SideA + SideB > SideC Then 'valid triangle
Return True
Else
Return False
End If
End Function
'--------------------------------------------------------------------
'Pre: sides a,b, and c correspond to a valid triangle
'post: Area of a triangle returned
'---------------------------------------------------------------------
Public Function Area() As Single
Return Math.Sqrt(Semiperimeter * (Semiperimeter - SideA) * (Semiperimeter - SideB) * (Semiperimeter - SideC))
End Function
'--------------------------------------------------------------------
'Returns the type (right, acute, obtuse) of an triangle
'Pre: Sides a,b, and c correpond to a valid riangle
'Post: type of a triangle returned
'-------------------------------------------------------------------
Public Function Type() As String
Select Case SideC^2
Case SideA^2 + SideB^2
Return "Right Triangle."
Case Is < SideA^2 + SideB^2
Return "Acute Triangle."
Case Is > SideA^2 + SideB^2
Return "Obtuse Triangle."
End Select
End Function
End Class
-------------------------------------------------------------------------------------------
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Calculate(ByVal sender As Object, ByVal e As EventArgs) Handles btnArea.Click, btnPerimeter.Click, btnType.Click
Dim sngSideA As Single = Val(Me.txtSideA.Text)
Dim sngSideB As Single = Val(Me.txtSideB.Text)
Dim sngSideC As Single = Val(Me.txtSideC.Text)
Dim UserTriangle As New Triangle(sngSideA, sngSideB, sngSideC)
If UserTriangle.ValidTriangle Then
Dim btnButtonClicked As Button = sender
Select Case btnButtonClicked.Tag
Case "Area"
Me.lblArea.Text = UserTriangle.Area
Case "Perimeter"
Me.lblPerimeter.Text = UserTriangle.s * 2
Case "Type"
Me.lblType.Text = UserTriangle.Type
End Select
Else
MessageBox.Show("Measurements are wrong")
Me.txtSideA.Text = Nothing
Me.txtSideB.Text = Nothing
Me.txtSideC.Text = Nothing
End If
End Sub
Private Sub SideChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSideA.TextChanged, txtSideB.TextChanged, txtSideC.TextChanged
Me.lblArea.Text = Nothing
Me.lblPerimeter.Text = Nothing
Me.lblType.Text = Nothing
End Sub
End Class
確定SOOOO我做的幾乎只能用代碼表示歉意,但我迷茫......有人請向我解釋爲什麼我的三角計算器沒有按」工作。該程序運行時沒有任何錯誤,如果在輸入任何值之前單擊其中一個按鈕,它將起作用。再次,我真的很抱歉...並感謝您提供的任何幫助。三角計算器vb.net,不明錯誤
當它不工作,會發生什麼? – xpda