What does this VB program mean?

2007-04-29 1:53 pm
For calculation of interest:


Private Sub btnYears_Click ( . . . ) Handles btnYears.Click
Compute years required to become a millionnaire
Dim balance as Double, numYears as Integer
balance = CDb1 (txtAmount.text)
Do While Balance < 1000000
balance += 0.06*balance
numYears +=1
Loop
txtWhen.Text = 'In "& numYears & " years you will have a mllion dollars."
更新1:

What does numYears +=1 mean? How does the program calculate the years to become a million dollars? Does the program recognise the variable numYears by its meaning? Thanks

回答 (1)

2007-04-29 4:23 pm
✔ 最佳答案
numYears += 1 is the same as numYears = numYears + 1
(vb.net suppots this C syntax)
Likewise balance += 0.06*balance is the same as
balance = balance +0.06*balance

Because numYears is declared as integer, its initial value is 0 without explicitly
assigning it a value.

numYears is a variable name which means nothing to the program (compiler). The data type of the variable is important to the compilation of the program.

In each iteration (loop), the balance is increased by 6%, and numYears increases by 1. numYears will only stop increasing when Balance exceeds 1000000.

The value of numYears after the loop is the number of times the loop has repeated and each iteration (loop) is literally one year, therefore numYears is the number of years it takes to become a millionaire.


收錄日期: 2021-04-25 20:32:48
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070429000051KK00682

檢視 Wayback Machine 備份