0

我有这个程序来计算纸张的成本。首先声明和初始化价格和捆绑包。然后我使用带有除法和模数的 if else 语句将捆绑包分解为收费率。尽管我的纸包数量计算正确,但我的总成本已减少。谁能发现我在这里做错了什么?

Module Paper
Sub Main()

    'declare variables
    Dim PaperAmountReq As Double
    Dim PricePer_1_Sheet As Double = 0.1
    Dim PricePer_100_Sheets As Double = 0.055
    Dim PricePer_500_Sheets As Double = 0.04
    Dim PricePer_1000_Sheets As Double = 0.03
    Dim NumberOfSingles As Double = 0
    Dim NumberOf100s As Double = 0
    Dim NumberOf500s As Double = 0
    Dim Numberof1000s As Double = 0

    Dim TotalCost As Double


    Console.Write("Enter number of sheets of paper needed: ")
    PaperAmountReq = Console.Readline


    If PaperAmountReq / 1000 >= 1 Then
        Numberof1000s = PaperAmountReq \ 1000
        PaperAmountReq = PaperAmountReq Mod 1000

    End If

    If PaperAmountReq / 500 >= 1 Then
        NumberOf500s = PaperAmountReq \ 500
        PaperAmountReq = PaperAmountReq Mod 500

    End If

    If PaperAmountReq / 100 >= 1 Then
        Numberof100s = PaperAmountReq \ 100
        PaperAmountReq = PaperAmountReq Mod 100

    End If

    If PaperAmountReq / 1 = PaperAmountReq Then
        NumberOfSingles = PaperAmountReq

    End If

    'TotalCost = (Convert.ToDouble(Numberof1000s) * PricePer_1000_Sheets) + (Convert.ToDouble(NumberOf500s) * PricePer_500_Sheets) + (Convert.ToDouble(Numberof100s) * PricePer_100_Sheets) + (Convert.ToDouble(NumberOfSingles) * PricePer_1_Sheet)
    TotalCost = (Numberof1000s * PricePer_1000_Sheets) + (NumberOf500s * PricePer_500_Sheets) + (Numberof100s * PricePer_100_Sheets) + (NumberOfSingles * PricePer_1_Sheet)


    Console.WriteLine("Number of 1000 packages: " & Numberof1000s)
    Console.WriteLine("Number of 500 packages: " & Numberof500s)
    Console.WriteLine("Number of 100 packages: " & Numberof100s)
    Console.WriteLine("Number of singles packages: " & NumberOfSingles)
    Console.Write("Total Cost: " & TotalCost)

End Sub

端模块

4

4 回答 4

2

有了这个:

If PaperAmountReq / 1000 >= 1 Then
    Numberof1000s = PaperAmountReq \ 1000
    PaperAmountReq = PaperAmountReq Mod 1000

End If

如果您有 1500,则设置Numberof1000s等于​​ 1 - 而不是 1000。因此,当您计算总成本时,您的(大概)成本/工作表的值乘以 1 而不是 1000。

要么在这里改变你的方程式

TotalCost = (Numberof1000s * PricePer_1000_Sheets) + (NumberOf500s * PricePer_500_Sheets) + (Numberof100s * PricePer_100_Sheets) + (NumberOfSingles * PricePer_1_Sheet)

成为类似的东西

TotalCost = (Numberof1000s * PricePer_1000_Sheets * 1000) + (NumberOf500s * PricePer_500_Sheets * 500) + (Numberof100s * PricePer_100_Sheets * 100) + (NumberOfSingles * PricePer_1_Sheet * 1)

或将PricePer_XXX_Sheets值调整为等效值。

Dim PricePer_1_Sheet As Double = 0.1
Dim PricePer_100_Sheets As Double = 0.055 * 100
Dim PricePer_500_Sheets As Double = 0.04 * 500
Dim PricePer_1000_Sheets As Double = 0.03 * 1000
于 2012-09-27T16:59:42.353 回答
2

那么你的变量数应该是整数,或者如果不够大,可能是 bigintegers,PaperAmountReq 也应该如此

您的代码似乎在这些段中假设整数除法

If PaperAmountReq / 500 >= 1 Then          
  NumberOf500s = PaperAmountReq \ 500          
  PaperAmountReq = PaperAmountReq Mod 500 
End if   

实际上,因为您使用的是双精度数,所以它正在执行浮点除法。

我还建议您使用十进制而不是双倍的价格?和总成本变量。

双打非常精确,但通常非常不准确。

于 2012-09-27T17:08:49.097 回答
0

看起来您需要将 NumberOfXXXs 变量转换为工作表,而不是捆绑包。

最后,您的公式使用每张成本和每捆数量。要么将成本更新为每个捆绑包,要么将捆绑包编号更新为等于该捆绑包中纸张的整数:

TotalCost = NumberOf1000s * 1000 * PricePer_1000_Sheets
于 2012-09-27T17:08:27.957 回答
0
Dim PricePer_100_Sheets As Double = 0.055 
Dim PricePer_500_Sheets As Double = 0.04 
Dim PricePer_1000_Sheets As Double = 0.03 

应该

Dim PricePer_100_Sheets As Double = 0.055 * 100
Dim PricePer_500_Sheets As Double = 0.04 * 500
Dim PricePer_1000_Sheets As Double = 0.03 * 1000
于 2012-09-27T17:14:11.077 回答