【问题标题】:Quadratic Equation Visual Basic 2010+二次方程 Visual Basic 2010+
【发布时间】:2015-07-12 10:24:19
【问题描述】:

我必须编写一个程序来计算二次方程并找到它的根。根必须通过MsgBox-es显示,变量A、B和C必须通过InputBox-es输入。现在我已经写了这个,但它不知何故不起作用,我不知道为什么。 **我是 Visual Basic 新手..

Public Class Form1
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim Det As Double
Dim x1 As Double
Dim x2 As Double


Private Sub txtA_Click(sender As Object, e As EventArgs) Handles txtA.Click
    txtA.Text = InputBox("Please, enter value of the variable A.", "Enter A")
End Sub

Private Sub txtB_Click(sender As Object, e As EventArgs) Handles txtB.Click
    txtB.Text = InputBox("Please, enter value of the variable B.", "Enter B")
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub cmdCalculate_Click(sender As Object, e As EventArgs) Handles cmdCalculate.Click
    A = Val(txtA.Text)
    B = Val(txtB.Text)
    C = Val(txtC.Text)
    Det = B ^ 2 + 4 * A * C
    If Det > 0 Then
        x1 = (-B + Math.Sqrt(Det)) / (2 * A)
        x2 = (-B - Math.Sqrt(Det)) / (2 * A)
        MsgBox("The roots are " + x1 + " and " + x2 + " ! ", 64, "2 Roots")
    ElseIf Det = 0 Then
        x1 = -B / (2 * A)
        MsgBox("The roots are " + x1 + " ! ", 64, "1 Double Root")
    ElseIf Det < 0 Then
        MsgBox("No roots ! ", 64, "No Roots")
    End If

End Sub

Private Sub txtC_Click(sender As Object, e As EventArgs) Handles txtC.Click
    txtC.Text = InputBox("Please, enter value of the variable C.", "Enter C")
End Sub

Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
    End
End Sub

Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
    txtA.Text = ""
    txtB.Text = ""
    txtC.Text = ""
End Sub

结束类

http://imgur.com/hIcDxFv

【问题讨论】:

    标签: visual-studio-2010 equation quadratic msgbox


    【解决方案1】:

    哦,问题只是因为 x1 和 x2 必须在 Str()..

    If Det > 0 Then
            x1 = (-B + Math.Sqrt(Det)) / (2 * A)
            x2 = (-B - Math.Sqrt(Det)) / (2 * A)
            MsgBox("The roots are " + Str(x1) + " and " + Str(x2) + " ! ", 64, "2 Roots")
        ElseIf Det = 0 Then
            x = -B / (2 * A)
            MsgBox("The roots are " + Str(x) + " ! ", 64, "1 Double Root")
    

    【讨论】:

      猜你喜欢
      • 2011-11-22
      • 2011-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      相关资源
      最近更新 更多