大家好,我得到了我的代码,这个号码最终是正确的唯一的事情是我无法让它循环正确。它应该询问用户是否愿意购买更多的酒吧,如果他们说是,它应该继续,如果它说不,那么它应该退出但我尝试的一切都不起作用。我开始认为这是我如何将信息输入程序,有人可以指出我在哪里搞砸了谢谢。
Sub Main()
' declare variable
Dim Answer As Char
Do
Console.Write("Would you like to buy some candy bars(1=Yes/0=No)? ")
Answer = CChar(Console.ReadLine())
Loop Until Answer = "1"
Console.WriteLine("Please enter the amount of candy you want to buy")
Console.WriteLine()
Dim amountDollar As Integer
Dim leftOverCoupons As Integer
Dim numberofChocolate As Integer
Dim freeChocolate As Integer
amountDollar = CInt(Console.ReadLine())
numberofChocolate = amountDollar
leftOverCoupons = numberofChocolate
While (leftOverCoupons >= 7)
freeChocolate = CInt(Math.Truncate(leftOverCoupons / 7))
numberofChocolate = numberofChocolate + freeChocolate
leftOverCoupons = (leftOverCoupons Mod 7) + freeChocolate
End While
Console.WriteLine("Total number of chocolate: " & numberofChocolate)
Console.WriteLine("Leftover Coupons: " & leftOverCoupons)
Console.ReadLine()
End Sub
答案 0 :(得分:3)
我在想你正在寻找这样的东西:
Sub Main()
' declare variable
Dim Answer As Char
Console.Write("Would you like to buy some candy bars(1=Yes/0=No)? ")
Answer = CChar(Console.ReadLine())
While (Answer = 1)
Console.WriteLine("Please enter the amount of candy you want to buy")
Console.WriteLine()
Dim amountDollar As Integer
Dim leftOverCoupons As Integer
Dim numberofChocolate As Integer
Dim freeChocolate As Integer
amountDollar = CInt(Console.ReadLine())
numberofChocolate = amountDollar
leftOverCoupons = numberofChocolate
While (leftOverCoupons >= 7)
freeChocolate = CInt(Math.Truncate(leftOverCoupons / 7))
numberofChocolate = numberofChocolate + freeChocolate
leftOverCoupons = (leftOverCoupons Mod 7) + freeChocolate
End While
Console.WriteLine("Total number of chocolate: " & numberofChocolate)
Console.WriteLine("Leftover Coupons: " & leftOverCoupons)
Console.Write("Would you like to buy more candy bars(1=Yes/0=No)? ")
Answer = CChar(Console.ReadLine())
End While
End Sub
答案 1 :(得分:0)
为什么你需要一个循环?一个简单的if语句,用于检查答案是否为1,否则为Application.Exit()
答案 2 :(得分:0)
究竟是什么不起作用?你提到如果用户选择“否”,则应该退出应用程序。但是,您的循环将继续,直到用户选择“是”:
Do
Console.Write("Would you like to buy some candy bars(1=Yes/0=No)? ")
Answer = CChar(Console.ReadLine())
Loop Until Answer = "1"
你甚至根本不需要循环。只需提示用户一次并阅读他们的回复。如果是“否”则退出应用程序。 (类似于Application.Exit()
。)如果是“是”,则继续使用其余的逻辑。
(另外,如果输入不是“否”或“是”,您可以重新提示,这可能涉及循环,或返回错误,或退出应用程序等。您想要做什么意外输入取决于您,但应予以考虑。)