2016-02-05 65 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

我嘗試按照您的建議進行錯誤更正。這是代碼: –