2011-03-09 121 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

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

相關問題