2014-08-27 89 views
-6

该程序将要求用户输入4次,然后用3个不同的答案显示3个消息框。每个答案包含2个不同的数字(范围),但我无法运行它,因为我无法使用该函数。为什么这不起作用?我找不到任何解决方案

我的主要问题是功能。变量a,b,c,d将由用户提供,x将在程序开始时由我提供。我无法运行该程序,因为函数带有蓝线下划线。

Dim a As Integer 
Dim b As Integer 
Dim c As Integer 
Dim d As Integer 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Dim a As Integer 
    Dim b As Integer 
    Dim c As Integer 
    Dim d As Integer 


Function f(x As Double) As Double 
    f = (a * x^3) + (b * x^2) + (c * x) + d 
    Exit Function 
End Function 
Sub incremental() 

    Dim left As Double 
    Dim right As Double 
    Dim product As Double 
    Dim counter As Integer 

    counter = 0 
    left = -10 
    right = -9.7 
    product = 1 

    a = InputBox("Please provide a coefficient for x^3.", "title", 0) 
    b = InputBox("Please provide a coefficient for x^2.", "title", 0) 
    c = InputBox("Please provide a coefficient for x^1.", "title", 0) 
    d = InputBox("Please provide a coefficient for x^0.", "title", 0) 

    Do While product > 0 
     product = f(left) * f(right) 

     If product > 0 Then 
      left = right 
      right = left + 0.3 

     Else 
      If counter = 0 Then 
       MsgBox("Your approximate root is " & left & " and " & right & ".") 
       counter = counter + 1 
       product = 1 
       left = right 
       right = left + 0.3 

      ElseIf counter = 1 Then 
       MsgBox("Your approximate root is " & left & " and " & right & ".") 
       counter = counter + 1 
       product = 1 
       left = right 
       right = left + 0.3 

      Else : counter = 2 
       MsgBox("Your approximate root is " & left & " and " & right & ".") 
       counter = 3 

      End If 

     End If 

    Loop 

End Sub 
+2

你的问题写得不好,或组织得不好。你的问题到底是什么? – Darrell 2014-08-27 18:48:30

+1

你有一个错字“'hhht”。 – 2014-08-27 18:49:19

+0

欢迎来到Stack Overflow。请参阅我们的文章[如何提出一个好问题](http://stackoverflow.com/help/how-to-ask)和[On-Topic Questions](http://stackoverflow.com/help/on-话题)。投入这些简短文件的时间将是值得的,因为通过遵循他们的建议,您可能会更快地得到更好的答案。要特别注意你的头衔 - 这个不能更模糊。 – JDB 2014-08-27 18:56:12

回答

1

安德鲁·阿诺德说:

roght = left + 0.3 

应该改成这样:

right = left + 0.3 
+0

+1发现,即使它(显然)不是OP的唯一问题。 – JDB 2014-08-27 19:31:00

4

不知道这是你的错别字只是其中一个或这实际上是问题,但是您缺少“End Sub”部分:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Dim a As Integer 
    Dim b As Integer 
    Dim c As Integer 
    Dim d As Integer 

' -------------------- 
End Sub ' <---------- 
' -------------------- 


Function f(x As Double) As Double 

这就是为什么Function以蓝色下划线。

但是这不会解决你的代码。由于您不了解面向对象编程的基础知识,因此它仍然存在问题。在这方面我可以提供的东西并不多,这只是一个主题。

例如,您声明Button1_Click来处理您的按钮点击。你说a,b,cd将由用户提供,但这似乎从未发生过(你没有分配任何东西给他们)。然后结束该功能,导致a,b,cd变量被删除(或更确切地说,这些变量所持有的存储器地址被释放)。

同时,你的函数f引用变量命名为abcd但从来没有宣布他们。看起来你试图使用Button1_Click函数中的变量,但Button1_Clickf是完全不同的上下文 - 它们不能访问彼此的变量。如果您想通过a,b,cdf,则必须将这些值作为参数(f(x, a, b, c, d))或通过其他方式(例如将它们声明为对象级属性等)来获取它们。

看起来这可能是一个编程作业,所以除了“祝你好运”之外,我不会再说什么。

相关问题