2016-02-05 66 views
0

我想用VB.NET简单计算在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) 
    Den = Int(TextBox2.Text) 
    hd = Int(TextBox4.Text) 
    vl = Int(TextBox5.Text) 
    re = (Den * vl * hd)/vl 
    TextBox3.Show(re) 

End Sub 

End Class 

见我的UI here

为什么我仍然收到错误信息“太多的论据”?

+2

如果要将're'发布到文本框,它是'TextBox3.Text = re.ToString()'。你应该打开Option Strict,但是 - (Den * v1 * hd)/ vl'的结果将是Double,而不是整数 – Plutonix

+0

如果我是正确的,那**不是如何计算雷诺数**。在尝试此操作之前,我会首先查看更多*** https://en.wikipedia.org/wiki/Reynolds_number*** ... – Codexer

+0

顺便说一句,'.Show()'只是将.Visible属性设置为true。 – Plutonix

回答

3

您发布的代码有几个错误,首先计算雷诺数是错误的。其次,请打开Option Strict与您当前的代码一样,它不会编译。三,请使用传统的命名约定这使得它从长远来看很难......还有更精彩的,但不是重点...

建议的解决方案

变量声明含义:

  • d =管的直径
  • v =流体流速的
  • U =液体
  • p =私室的Viscoscity液体
  • 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 
    
+0

'TextBox3.Show(tot.ToString)'? – Plutonix

+1

@Plutonix的意思是添加一个'MessageBox' ...很好的抓住:) – Codexer

+3

为什么不告诉他如何把它放在一个巧妙命名的文本框(例如'TextBox13'),因为那是他的真正问题 – Plutonix

0

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 

我不打算说明您的公式是否正确。

+3

只是一个建议, TryParse'是错误的,你正在使用。它需要一个字符串和整数,而不是整数/字符串。 – Codexer

+0

是的。我只是输入它而不回到IDE来验证。我将编辑答案。 –

+0

我尝试按照您的建议进行错误更正。这是代码: –