有人能告诉我我做错了什么

时间:2011-03-09 18:38:56

标签: vb.net

我得到的输出并不是我想要的,当你输入50美元时我应该得到58个酒吧和2个优惠券但是当我运行它时我得到57个剩下7个优惠券。谢谢。

Module Module1

    Sub Main()
        ' declare variable
        Dim amountDollar As Integer
        Dim totalCoupons As Integer
        Dim leftOverCoupons As Integer
        Dim numberofChocolate As Integer
        Dim freeChocolate As Integer
        Dim totalchocolate As Integer
        Console.WriteLine("Please enter your amount here")
        amountDollar = CInt(Console.ReadLine())
        numberofChocolate = amountDollar
        freeChocolate = CInt(numberofChocolate / 7)
        totalchocolate = numberofChocolate + freeChocolate
        Console.WriteLine("Total number of chocolate: " & totalchocolate)
        leftOverCoupons = freeChocolate Mod numberofChocolate
        Console.WriteLine("Leftover Coupons: " & leftOverCoupons)
    End Sub

End Module

好的,这是新的,但我能做到的将是mod和divison为什么是

             Sub Main()
                ' This program will calculate the number of chocolate bars you can buy        or redeem by coupons for an input number
                   ' of dollars. Each bar costs one dollar and there is one coupon in each bar. It takes 7 coupons to receive one
                 ' additional chocolate bar.
             Dim dollars, numberOfBars, couponCount As Integer
              ' Prompt user to enter amount of money they have to spend
            Console.WriteLine("Would you like to buy candy bars")
             Console.WriteLine("Yes")
             Console.WriteLine("No")
             Console.WriteLine("Please enter your amount here")
             dollars = Convert.ToInt32(Console.ReadLine())
            ' Set value of numberOfBars and couponCount
               numberOfBars = dollars
    couponCount = numberOfBars
    ' Begin loop. Loop will determine total bars and remaining coupons
    Do While couponCount >= 7
        couponCount = couponCount - 7
        numberOfBars = numberOfBars + 1
        couponCount = couponCount + 1
    Loop

    ' Output values for ending number of chocolate bars and remaining coupons
    Console.WriteLine("The total number of chocolate bars you receive is: " & numberOfBars)
    Console.WriteLine("The number of coupons you have left is: " & couponCount)

End Sub

Okey我已经弄清楚如果有人输入No,我该如何停止该程序?

1 个答案:

答案 0 :(得分:0)

对于58与57相比,我认为57实际上是你想要的 - 50条加(50/7)= 7.14轮到7 = 57

如果有人只购买1个酒吧,你不会希望他们获得免费酒吧,不是吗?

如果你确实想要58,那么你需要使用Double's来计算数学,你需要使用Math.Ceiling来获得下一个数字。

对于剩下的优惠券数量,我认为您需要更改:

leftOverCoupons = freeChocolate Mod numberofChocolate

leftOverCoupons = freeChocolate Mod 7

为了提高代码的可读性,你应该为这个“神奇数字”7定义一个局部常量