該程序應該讀取文本文件中的值並獲取所有這些值的總和。然後使用從一系列複選框和文本框中收集的信息來計算最終利潤。在Visual Basic中計算利潤
由於現在編寫代碼,如果所有複選框都被選中,利潤是正確的,但是如果選中了一個,兩個或全部三個,我需要它是正確的。下面是當前的代碼 選項嚴格在
Imports System.IO
Public Class Form1
Dim sum As Double
Dim fileRead As Boolean
Dim profit As Double
Private Sub menOpen_Click(sender As Object, e As EventArgs) Handles menOpen.Click
Dim ofd As New OpenFileDialog
ofd.Filter = "text files |*.txt|All Files|*.*"
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim selectedFileName As String = System.IO.Path.GetFileName(ofd.FileName)
If selectedFileName.ToLower = "profit.txt" Then
Dim line As String
Using reader As New StreamReader(ofd.OpenFile)
While Not reader.EndOfStream
line = reader.ReadLine
Dim value As Integer
If Integer.TryParse(line, value) Then
sum = sum + value
fileRead = True
End If
Console.WriteLine(line)
End While
End Using
Else
MessageBox.Show("You cannot use that file!")
End If
End If
End Sub
Private Sub menExit_Click(sender As Object, e As EventArgs) Handles menExit.Click
Me.Close()
End Sub
Private Sub radSales_CheckedChanged(sender As Object, e As EventArgs) Handles radSales.CheckedChanged
If radSales.Checked Then
profit = sum
End If
End Sub
Private Sub radSandO_CheckedChanged(sender As Object, e As EventArgs) Handles radSandO.CheckedChanged
If radSandO.Checked Then
If Trim(txtWages.Text) = "" Then
txtWages.Text = CStr(0)
End If
profit = (sum - CDbl(txtWages.Text) - CDbl(txtRent.Text) - CDbl(txtUtilities.Text))
End If
End Sub
Private Sub menComputeProfit_Click(sender As Object, e As EventArgs) Handles menComputeProfit.Click
If fileRead = False Then
MessageBox.Show("The file profit.txt has not been read in yet, the profit will be set to zero.")
sum = 0
End If
If chkWages.Checked Then
profit = CDbl(("$" & Val(sum) - (Val(txtWages.Text) + Val(txtRent.Text) + Val(txtUtilities.Text))))
End If
If chkRent.Checked Then
profit = CDbl(("$" & Val(sum) - (Val(txtRent.Text) + Val(txtWages.Text) + Val(txtUtilities.Text))))
End If
If chkUtilities.Checked Then
profit = CDbl(("$" & Val(sum) - (Val(txtUtilities.Text) + Val(txtWages.Text) + Val(txtRent.Text))))
End If
txtAnswer.Text = profit.ToString
End Sub
End Class
任何幫助將不勝感激。
我想你已經檢查了文本框只包含數字和小數點。 – Mych
請記住在VB.NET中計算利潤以取得Micro $ oft的楔子! – Paul
我認爲你的利潤分配有點不對。嘗試從混音中刪除「$」,並在分配給文本框時將其添加到字符串中。 – Paul