VB.Net中的简单计算
我想用VB.NET计算雷诺数 这是我的代码:VB.Net中的简单计算,vb.net,visual-studio,Vb.net,Visual Studio,我想用VB.NET计算雷诺数 这是我的代码: Public Class Form1 Dim vis As Integer Dim Den As Integer Dim hd As Integer Dim vl As Integer Dim re As Integer Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click vis = Int(TextBox1.Text)
Public Class Form1
Dim vis As Integer
Dim Den As Integer
Dim hd As Integer
Dim vl As Integer
Dim re As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
vis = Int(TextBox1.Text)
Den = Int(TextBox2.Text)
hd = Int(TextBox4.Text)
vl = Int(TextBox5.Text)
re = (Den * vl * hd) / vl
TextBox3.Show(re)
End Sub
End Class
查看我的用户界面
为什么我仍然收到一条错误消息“参数太多”?您发布的代码中有一些错误,首先,雷诺数的计算是错误的。第二,请打开
选项Strict
,因为当前代码不会编译。第三,请使用传统的命名约定,从长远来看,这会很困难。。。还有更多,但不是重点
建议的解决方案
变量声明含义:
- d=管道直径
- v=液体速度
- u=液体的粘度
- p=液体的密度
- tot=雷诺数
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim d,v,u,p,tot As Single If Single.TryParse(TextBox1.Text,d) AndAlso Single.TryParse(TextBox2.Text,v) AndAlso Single.TryParse(TextBox3.Text,u) AndAlso Single.TryParse(TextBox1.Text,p) Then tot = (d * v * p) / (u * 0.001) MessageBox.Show(tot.ToString) 'OR TextBox3.Text = tot.ToString End If End Sub
选项Strict
,因为当前代码不会编译。第三,请使用传统的命名约定,从长远来看,这会很困难。。。还有更多,但不是重点
建议的解决方案
变量声明含义:
- d=管道直径
- v=液体速度
- u=液体的粘度
- p=液体的密度
- tot=雷诺数
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim d,v,u,p,tot As Single If Single.TryParse(TextBox1.Text,d) AndAlso Single.TryParse(TextBox2.Text,v) AndAlso Single.TryParse(TextBox3.Text,u) AndAlso Single.TryParse(TextBox1.Text,p) Then tot = (d * v * p) / (u * 0.001) MessageBox.Show(tot.ToString) 'OR TextBox3.Text = tot.ToString End If End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Integer.TryParse(TextBox1.Text, vis) AndAlso _
Integer.TryParse(TextBox2.Text, Den) AndAlso _
Integer.TryParse(TextBox4.Text, hd) AndAlso _
Integer.TryParse(TextBox5.Text, vl) Then
'Do your calculation
Else
'There is some kind of error. Don't do the calculation
End If
End Sub
我不打算讨论您的公式是否正确。Int函数不进行类型转换。它只返回值的整数部分(14.8将变为14)。要进行此转换,如果您保证传入的文本确实是一个数字,则需要使用CInt 由于您使用的是用户提供的值,因此可能需要使用一些错误更正
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Integer.TryParse(TextBox1.Text, vis) AndAlso _
Integer.TryParse(TextBox2.Text, Den) AndAlso _
Integer.TryParse(TextBox4.Text, hd) AndAlso _
Integer.TryParse(TextBox5.Text, vl) Then
'Do your calculation
Else
'There is some kind of error. Don't do the calculation
End If
End Sub
我不想讨论你的公式是否正确。如果你想将
re
发布到文本框,它是TextBox3.Text=re.ToString()
。但是,您应该启用选项Strict(严格)-(Den*vl*hd)/vl
的结果将是双精度,而不是整数如果我是正确的,这不是计算雷诺数的方法。在尝试此操作之前,我会先查看更多内容…顺便说一句,.Show()
只是将.Visible属性设置为true。非常感谢。程序现在可以运行了。我将按照您的建议更正公式。如果您想将re
发布到文本框中,它就是TextBox3.Text=re.ToString()
。但是,您应该启用选项Strict(严格)-(Den*vl*hd)/vl
的结果将是双精度,而不是整数如果我是正确的,这不是计算雷诺数的方法。在尝试此操作之前,我会先查看更多内容…顺便说一句,.Show()
只是将.Visible属性设置为true。非常感谢。这个程序现在可以运行了。我将按照你的建议纠正这个等式。TextBox3.Show(tot.ToString)
?@puropoix的意思是添加一个MessageBox
…很好的捕获:)为什么不告诉他如何将它放在一个命名巧妙的文本框中(例如TextBox13
),因为这是他的实际问题TextBox3.Show(tot.ToString)
?@puropoix打算添加一个消息框
…很好的捕获:)为什么不告诉他如何将其放入一个命名巧妙的文本框(例如TextBox13
)因为这是他的实际问题只是一个建议,你使用的TryParse
是错误的。它需要一个字符串和整数,而不是整数/字符串。我只是在没有返回IDE进行验证的情况下输入了它。我将编辑答案。我尝试按照您的建议进行错误更正。这是代码:只是一个建议,您使用的TryParse
是错误的。它需要一个字符串和整数,而不是整数/字符串。我只是在没有返回IDE进行验证的情况下输入了它。我将编辑答案。我尝试按照您的建议进行错误更正。代码如下: