2011-03-09 78 views
0

我得到的输出是不是当我输入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 

确定这是新的,但我能做到这一点会在国防部和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 

欧凯我必须想出办法来的只是如何停止的程序,如果有人进入没有?

+0

你需要给你更多的家庭作业规范。 50 +(50/7)当四舍五入到最接近的整数时肯定是57。 – geoffspear

+0

嗯,我想能够得到这个输出20美元给23条和2优惠券剩下或50美元会给你留下58酒吧和2张优惠券。我一直在这一天工作。我被要求使用整数除法来确定兑换的优惠券数量,然后我将需要使用MOD来确定我将剩下多少优惠券。 – norris1023

回答

0

为58比57,我认为57其实就是你在找什么 - 50条加(50/7)= 7.14回合下来到7 = 57

在有人只买1的情况下酒吧,你不希望他们得到一个免费的酒吧,是吗?

如果你想要58,那么你需要使用Double的数学,你需要使用Math.Ceiling来获得下一个数字。

左优惠券的数量,我认为你需要改变:

leftOverCoupons = freeChocolate Mod numberofChocolate 

leftOverCoupons = freeChocolate Mod 7 

改进代码的可读性一点,你应该还定义本地不断为这个“幻数”7

+0

......他应该分享一些美味的巧克力。 :-) –

相关问题