无法弄清楚计算

时间:2016-02-05 07:49:01

标签: vb.net basic

我需要制定一个电话帐单程序,允许用户计算他们的电话帐单。他们会输入他们的会议记录,文本和数据限额以及他们使用了多少。

每增加一分钟为30p,每个额外文本为10p,超过限制使用的数据为10.00英镑,全部加到最终费用中。

问题是我无法让程序计算每分钟的成本,我想在找出分钟的计算方法后,我将能够对文本和数据做同样的事情。

感谢您的帮助,非常感谢。

UI

This is the problem

Public Class Form1     Private Sub Form1_Load(sender as Object,e As EventArgs)处理MyBase.Load     结束子

Private Sub btn1TotalCost_Click(sender As Object, e As EventArgs) Handles btn1TotalCost.Click

    Dim MinutesAllowed As Integer
    Dim MinutesUsed As Integer
    Dim TextsAllowed As Integer
    Dim TextsUsed As Integer
    Dim DataAllowed As Integer
    Dim DataUsed As Integer
    Dim MinutesTotalCost As Double
    Dim TextsTotalCost As Double
    Dim DataTotalCost As Double
    Dim MonthlyCost As Double
    Dim TotalCost As Double

    MinutesAllowed = Val(txtBoxMinutesAllowed.Text)
    MinutesUsed = Val(txtBoxMinutesUsed.Text)
    TextsAllowed = Val(txtBoxTextsAllowed.Text)
    TextsUsed = Val(txtBoxTextsUsed.Text)
    DataAllowed = Val(txtBoxDataAllowed.Text)
    DataUsed = Val(txtBoxDataUsed.Text)
    MinutesTotalCost = Val(txtBoxTotalCost.Text)
    TextsTotalCost = Val(txtBoxTotalCost.Text)
    DataTotalCost = Val(txtBoxTotalCost.Text)
    TotalCost = Val(txtBoxTotalCost.Text)

    'Minutes
    MinutesAllowed = Integer.Parse(txtBoxMinutesAllowed.Text)
    MinutesUsed = Integer.Parse(txtBoxMinutesUsed.Text)
    MonthlyCost = Val(txtBoxMonthlyCost.Text)

    If MinutesAllowed >= MinutesUsed Then
        MinutesTotalCost = 0
    Else
        MinutesTotalCost = (MinutesUsed - MinutesAllowed) * 0.3
    End If
    txtBoxTotalCost.Text = CType(MinutesTotalCost + MonthlyCost, String)

    'Texts
    TextsAllowed = Integer.Parse(txtBoxTextsAllowed.Text)
    TextsUsed = Integer.Parse(txtBoxTextsUsed.Text)
    MonthlyCost = Val(txtBoxMonthlyCost.Text)

    If TextsAllowed >= TextsUsed Then
        TextsTotalCost = 0
    Else
        TextsTotalCost = (TextsUsed - TextsAllowed) * 0.15
    End If
    txtBoxTotalCost.Text = CType(TextsTotalCost + MonthlyCost, String)

    'Data
    DataAllowed = Integer.Parse(txtBoxDataAllowed.Text)
    DataUsed = Integer.Parse(txtBoxDataUsed.Text)
    MonthlyCost = Val(txtBoxMonthlyCost.Text)

    If DataAllowed >= DataUsed Then
        DataTotalCost = 0
    Else
        DataTotalCost = (DataUsed - DataAllowed) + 10.0
    End If
    txtBoxTotalCost.Text = CType(DataTotalCost + MonthlyCost, String)

End Sub

结束班

1 个答案:

答案 0 :(得分:1)

MinutesTotalCost = MinutesUsed - MinutesAllowed * 0.3

乘法和除法运算符优先于加法和减法运算符 您需要在计算中添加括号。

MinutesTotalCost = (MinutesUsed - MinutesAllowed) * 0.3

您还会在此处覆盖计算结果:

MinutesTotalCost = txtBoxTotalCost.Text

编辑:只需分钟的工作代码

 Dim MinutesAllowed As Integer
 Dim MinutesUsed As Integer
 Dim MinutesTotalCost As Double
 Dim MonthlyCost As Double

 MinutesAllowed = Integer.Parse(txtBoxMinutesAllowed.Text)
 MinutesUsed = Integer.Parse(txtBoxMinutesUsed.Text)
 MonthlyCost = Val(txtBoxMonthlyCost.Text)

 If MinutesAllowed >= MinutesUsed Then
     MinutesTotalCost = 0
 Else
     MinutesTotalCost = (MinutesUsed - MinutesAllowed) * 0.3         
 End If
 txtBoxTotalCost.Text = CType(MinutesTotalCost + MonthlyCost, String)

Val()返回一个double,因此您需要使用Integer.Parse()作为int Double需要转换为文本框的字符串。

编辑2:
由于MonthlyCost不会改变,因此需要

MonthlyCost = Val(txtBoxMonthlyCost.Text)

一开始只有一次 您始终会覆盖txtBoxTotalCost的值。只需用所有费用进行单一计算,即

 txtBoxTotalCost.Text = CType(MinutesTotalCost + TextsTotalCost + DataTotalCost + MonthlyCost, String)