+ -
当前位置:首页 → 问答吧 → VB简单的小问题

VB简单的小问题

时间:2011-08-30

来源:互联网

VB code

Option Explicit

Private Function More(x As Integer) As Long
More = Fix(x) + 1
End Function

Private Sub Command1_Click()
Text1.Text = More(Text1.Text)
End Sub



只有两句代码,定义了一个函数More(x),

函数的效果是用“进一法”取x的近似值,

想法就是用Fix舍弃小数部分,再将整数部分+1

但是运行后,在文本框中输入5.123得到6,正常

  输入5.789得到7,为什么?

作者: mouse_event   发布时间: 2011-08-30

fix(5.78)=5
而在你的例子中,More函数所定义的参数x为Integer类型,也就是说,在More=Fix(X)+1之前,经过了一个数到x类型的转换过程。等同于:
VB code

Private Sub Command1_Click()
dim temp as integer
temp =cint(text1.text)
Text1.Text = More(temp)
End Sub


也就是说,你在进入More函数时,X应该=6,而不是5.78 ,所以返回值是7

把More函数的参数,改成double,就可以得到你所要的结果。

作者: BestBadGod   发布时间: 2011-08-30

VB code

Option Explicit



Private Function More(x As Double) As Long
    More = Fix(x) + 1
End Function

Private Sub Command1_Click()
    Text1.Text = More(Text1.Text)
End Sub

作者: Veron_04   发布时间: 2011-08-30

Private Function More(x As Integer) As Long

问题出在红色部分,你的x类型不能是Integer类型

作者: Veron_04   发布时间: 2011-08-30