how to make this into a nest loop
Hello all I got my code and the number are finally right the only thing is i can not get it to loop right. It should ask the user if they would like to buy more bars and if they say yes it should continue if it says no then it should exit but everything i am trying is not working. I am starting to think it is how i am putting the information into the program can someone point out where i am screwing up at thanks.
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
I'm thinking you're looking for something like this:
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
Exactly what isn't working? You mention that if the user selects "no" then the application should exit. However, your loop is continuing until the user selects "yes":
Do
Console.Write("Would you like to buy some candy bars(1=Yes/0=No)? ")
Answer = CChar(Console.ReadLine())
Loop Until Answer = "1"
You don't even really need a loop for this at all. Just prompt the user once and read their response. If it's "no" then exit the application. (Something like Application.Exit()
.) If it's "yes" then continue with the rest of the logic.
(Additionally, if the input is something other than "no" or "yes" you can either re-prompt, which may involve a loop, or return an error, or exit the application, etc. What you want to do with unexpected input is up to you, but should be considered.)
Why do you need a loop at all? A simple if statement to check if the answer is 1 else Application.Exit()
精彩评论