简体   繁体   中英

Vbscript loop, if then issue

I need some assistance. I am new to vbscript and I have a problem/question.

I am trying to give an end user the ability to start a program over. I have done research on MSDN and googled various other sites that involve tutorials based on if then statements, subroutines, functions and do,while,until loops

all the loop tutorials or explanations have a program looping through numbers and string lengths. I have developed a common stock transaction program:

'Stock Transaction Program

Option Explicit
Dim stockPrice, commission, stocksBought, stockCommission, stockTotal, totalCost, tryAgain


Wscript.StdOut.Write "Stock price: "
stockPrice = CDbl(Wscript.StdIn.ReadLine)
Wscript.StdOut.Write "Number of stocks bought: "
stocksBought = CDbl(Wscript.StdIn.ReadLine)
Wscript.StdOut.Write "Stock Commission percentage in whole number (i.e 2 for 2%): "
commission = CDbl(Wscript.StdIn.ReadLine)

stockTotal = stockPrice * stocksBought
stockCommission = commission * stockTotal / 100
totalCost = stockCommission + stockTotal

Wscript.StdOut.WriteLine "You payed a total commission of $" & stockCommission & " on the stocks you bought."
Wscript.StdOut.WriteLine "Your total price for you purchasing " & stocksBought & " stocks is $" & totalCost & "."

After this program concludes, I would to give the end user an option to evaluate another stock price..etc.

Wscript.Stdout.Writeline "Would you evaluate another stock price? (i.e y or n): "
tryAgain = lcase(wscript.stdin.readline)

My logic assumes that you would now proceed with an if statement of some kind that basically says: that if tryAgain equals "y" then proceed back to the top, if "n" then exit program with a thank you conclusion, if invalid character then state that the end user entered an invalid response, allowing them the oppurtunity to try again.

I couldn't figure it out. functions and subroutines need arguments (i assume) and subroutines do not give data output. functions do but this code doesnt classify. I was hoping that their was a syntax that would allow the program to start over. I think a loop of some kind is probably the answer but i dont have the knowledge or the experience.

Is their anyone that can assist me???

Thanks

This could be one of the simplest solutions.

Option Explicit
Dim stockPrice, .... , tryAgain

Do
    '....
    ' your stocks code
    '....

    Wscript.Stdout.Writeline "Would you evaluate another stock price? (i.e y or n): "
    tryAgain = lcase(wscript.stdin.readline)

Loop While tryAgain="y"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM