Private Sub Command1_Click()
Dim n As Integer, s As Integer
s = 0
For n = 0 To 24
If Check1(n).Value = 1 Then
s = s + 0.9
ElseIf Check1(n).Value = 0 Then
s = s
End If
Next n
Dim m As Integer, i As Integer
i = 0
For m = 0 To 24
If Check2(m).Value = 1 Then
i = i + 0.6
ElseIf Check2(m).Value = 0 Then
i = i
End If
Next m
jstxt.Text = (s + i) / 25 怎么算出来总是1啊?
vb程序中check控件问题
答案:2 悬赏:0
解决时间 2021-03-08 04:34
- 提问者网友:迷茫庸人
- 2021-03-07 17:37
最佳答案
- 二级知识专家网友:我叫很个性
- 2021-03-07 18:35
这是因为你把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 最后这个如果你能自己看出它的原理,那么对你的编程技能将有很大提高!
另外像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 最后这个如果你能自己看出它的原理,那么对你的编程技能将有很大提高!
全部回答
- 1楼网友:恕我颓废
- 2021-03-07 18:45
控件:text1,check1.代码如下。
===========
option explicit
private const swp_nosize = &h1
private declare function setparent lib "user32" (byval hwndchild as long, byval hwndnewparent as long) as long
private declare function setwindowpos lib "user32" (byval hwnd as long, byval hwndinsertafter as long, byval x as long, byval y as long, byval cx as long, byval cy as long, byval wflags as long) as long
private sub form_load()
setparent check1.hwnd, text1.hwnd
setwindowpos check1.hwnd, 0, 0, 0, 0, 0, swp_nosize
end sub
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯