我是VB新手 制了一个百分比计算器 可是输入数据相差大一点就计算出错,请高手帮我看一下,最好帮我改改语句,我设计的是上面3个框输入数字计算结果在下面三个框中显示。
Private Sub Command1_Click()
Dim a As Double
Dim b As Double
Dim c As Double
Let a=Val (Text1.Text)
Let b=Val (Text2.Text)
Let c=Val (Text3.Text)
Text4.Text=a/(a+b+c)
Text5.Text=b/(a+b+c)
Text6.Text=c/(a+b+c)
请VB高手来,为什么百分比计算老出错
答案:4 悬赏:40
解决时间 2021-03-03 02:37
- 提问者网友:江山如画
- 2021-03-02 21:24
最佳答案
- 二级知识专家网友:瘾与深巷
- 2021-03-02 22:43
改为如下代码:
Private Sub Command1_Click()
Dim a As Double
Dim b As Double
Dim c As Double
Let a = Val(Text1.Text)
Let b = Val(Text2.Text)
Let c = Val(Text3.Text)
Text4.Text = (Int(10000 * a / (a + b + c)) / 100) & "%"
Text5.Text = (Int(10000 * b / (a + b + c)) / 100) & "%"
Text6.Text = (Int(10000 * c / (a + b + c)) / 100) & "%"
If Len(Text4.Text) < 5 Then Text4.Text = "0" & Text4.Text
If Len(Text5.Text) < 5 Then Text5.Text = "0" & Text5.Text
If Len(Text6.Text) < 5 Then Text6.Text = "0" & Text6.Text
End Sub
Private Sub Command1_Click()
Dim a As Double
Dim b As Double
Dim c As Double
Let a = Val(Text1.Text)
Let b = Val(Text2.Text)
Let c = Val(Text3.Text)
Text4.Text = (Int(10000 * a / (a + b + c)) / 100) & "%"
Text5.Text = (Int(10000 * b / (a + b + c)) / 100) & "%"
Text6.Text = (Int(10000 * c / (a + b + c)) / 100) & "%"
If Len(Text4.Text) < 5 Then Text4.Text = "0" & Text4.Text
If Len(Text5.Text) < 5 Then Text5.Text = "0" & Text5.Text
If Len(Text6.Text) < 5 Then Text6.Text = "0" & Text6.Text
End Sub
全部回答
- 1楼网友:开心就好
- 2021-03-03 00:22
private sub command1_click()
msgbox "百分比为:" & format(text1 * 100 / text2, "00.00") & "%"
end sub
text1 text2中输入数字,分别为分子和分母
- 2楼网友:茫然不知崩溃
- 2021-03-03 00:14
Private Sub Command1_Click()
Dim a As Double
Dim b As Double
Dim c As Double
Let a = Val(Text1.Text)
Let b = Val(Text2.Text)
Let c = Val(Text3.Text)
If a + b + c <> 0 Then
Text4.Text = a / (a + b + c)
Text5.Text = b / (a + b + c)
Text6.Text = c / (a + b + c)
End If
End Sub
- 3楼网友:旧事诱惑
- 2021-03-02 23:05
有可能是你将数据定义为double求解容易产生溢出错误导致结果错误。改为float即可。即
Private Sub Command1_Click()
Dim a As float
Dim b As float
Dim c As float
Let a=Val (Text1.Text)
Let b=Val (Text2.Text)
Let c=Val (Text3.Text)
Text4.Text=a/(a+b+c)
Text5.Text=b/(a+b+c)
Text6.Text=c/(a+b+c)
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯