我正在创建可以解决二次方程式的代码,但是我不断收到此错误。每次高亮显示x =函数。我不知道这是怎么回事,请帮忙。代码如下。
Option Explicit
Sub main()
Dim a As Double
Dim b As Double
Dim c As Double
Dim x1 As Long
a = InputBox("Write a number for a")
b = InputBox("Write a number for b")
c = InputBox("Write a number for c")
x1 = (-b + Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
MsgBox (x1)
End Sub
编辑:由于我的帮助,我得以运行该程序。但是,当我将x1和x2转换为方程式时,我得到的数字似乎没有意义。
Option Explicit
Sub main()
Dim a As Double
Dim b As Double
Dim c As Double
Dim x1 As Double
Dim x2 As Double
a = InputBox("Write a number for a")
b = InputBox("Write a number for b")
c = InputBox("Write a number for c")
'If statement to check if items inside sqr are negative. Being negative would create an imaginary number, and we only need real numbers.
If (b ^ 2) - (4 * a * c) < 0 Then
MsgBox ("The selected numbers for a, b, and c would make an imaginary number. Please try again.")
'If the selected values for abc do not create an imaginary number, the equation is run giving the two values of the x's.
Else
x1 = (-b + Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
x2 = (b + Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
'Msgbox showing the equation with the values for abc and the values for x1 and x2.
MsgBox (a & "(x^2)+" & b & "x+" & c & vbNewLine & "x1=" & x1 & vbNewLine & "x2=" & x2)
End If
End Sub
编辑:没关系。我有一个负面的开关。谢谢。
包括对虚数的检查:
Sub main()
Dim a As Double
Dim b As Double
Dim c As Double
Dim x1 As Double
a = InputBox("Write a number for a")
b = InputBox("Write a number for b")
c = InputBox("Write a number for c")
If (b ^ 2) - (4 * a * c) < 0 Then
MsgBox "The square root of ((" & b & " ^ 2) - (4 * " & a & " * " & c & ")) = (" & (b ^ 2) - (4 * a * c) & ") would make an imaginary number."
Else
x1 = (-b + Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
MsgBox x1
End If
End Sub
这是另一个替代方案:
使用ImSqrt以文本x + yi
或x + yj
文本格式返回复数的平方根-但是它可能相当“复杂”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句