这是因为你把s和i都声明为整形变量(Integer),整数你应该知道是什么吧?就是不带小数的数,VB会自动把s和i的小数去掉(大于0.5的就进1),所以 s=s+0.9 就跟 s=s+1 一样!i=i+0.6 也跟i=i+1一样!
另外像s=s和i=i这样的看起来很傻的,完全是多此一举啊!
改为这样:
Private Sub Command1_Click()
Dim n As Integer, s As Single 's设为单精度浮点数,这样才支持小数
s = 0 's的初始值就是0,所以这行是可以删掉的
For n = 0 To 24
If Check1(n).Value = 1 Then '只需判断=1这个即可,=0就不用处理了
s = s + 0.9
End If
Next n
Dim i As Single 'i也设为单精度浮点数。另外上面定义的n可以重复使用的,无须再声明m
i = 0 '同理这行也可以删掉
For n = 0 To 24
If Check2(n).Value = 1 Then '同上
i = i + 0.6
End If
Next n
jstxt.Text = (s + i) / 25
另外,程序如果要优化到最简单,可以这样:
Private Sub Command1_Click()
Dim n As Integer, s As Single, i As Single
For n = 0 To 24
If Check1(n).Value = 1 Then s = s + 0.9
If Check2(n).Value = 1 Then i = i + 0.6
Next n
jstxt.Text = (s + i) / 25
还有更简单的:
Private Sub Command1_Click()
Dim n As Integer, s As Single, i As Single
For n = 0 To 24
s = s + 0.9 * Check1(n).Value
i = i + 0.6 * Check2(n).Value
Next n
jstxt.Text = (s + i) / 25
最后这个如果你能自己看出它的原理,那么对你的编程技能将有很大提高!
把工程做为附件传上来