Need help with vb.net

Status
Not open for further replies.

z0ltan

Baseband Member
Messages
22
hey I am taking a programming and logic class and got a little assignment that im having trouble with. im sure the solution will be easy but at the moment i cant figure it out.

[FONT=&quot]3. Consider a bank account with a Deposit of $ 1000.00 upon opening and another deposit of a 1000.00 at the end of every year.
The Interest rate is a 5 %. Use a Do while statement to calculate how long would take you to save a 1000.000.00[/FONT]

Public Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Balance, Deposit, intrest As Decimal
Dim years, numyears As Integer

Balance = 1000.0
Deposit = 1000.0
intrest = 0.05
years = 0

Do While Balance < 1000000.0
Balance = ((deposit + balance) *.05) + balance) then
numyears = years + 1


MsgBox("It will take " & numyears & " years to become a millionare")
Loop

End Sub
End Class

thats the code i have right now and am having trouble. maybe im taking the wrong approach but i cant figure it out. I dont want the answer, or the code that makes it work, could someone just give me some hints/ideas on what to try to change, or like a different way to approach the problem. thanks
 
I've not used VB.net but I can tell you a few problems that jump out at me:

Code:
numyears = years + 1
This would be saying "numyears = 0 + 1" regardless, you probably want to use your initial variable.
Code:
years = years + 1

Also, I'm not sure if VB.net uses short-hand, years++ and/or years += 1 might work.

Code:
Balance = ((deposit + balance) *.05) + balance)
I'm not sure about this, but I think it should be -
5% of the current balance, plus the current balance, plus the new deposit. (Since banks won't give you a year's worth of interest for a new deposit...dang, I wish...), so maybe...
Code:
Balance = .05 * balance + balance + deposit

Also a side note, you have an interest variable - so you probably should use that instead of the .05 literal. :)

Also for general logic, (again I've not used VB!), the MsgBox should probably be outside of the loop, so it appears after the do while finishes - and using the years variable instead.

Hope that helps some.
 
i worked with VB before, and the tips i can give is, firstly you must design how you want the program to be looked like. if you just doing a simple calculation on that, present the input clearly.

i believe you know how to add the value from the TextBox to a variable, then you just playing with the variable. if your assignment is quite flexible, you might be able to improve your answer by doing such a thing, rather than just having the same number.

prepare the formula,
Balance = Balance + Deposit
Balance = balance x 1.05
then count on the year. take note what Vormund has told you...

let me know if this helps you...
 
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Balance As Decimal = 1000.0
Dim Deposit As Decimal = 1000.0
Dim intrest As Decimal = 0.05
Dim years As Integer = 0

Do While Balance < 1000000.0

Balance = ((Deposit + Balance) * 0.05) + Balance
years = years + 1



Loop
MsgBox("It will take " & years & " years to become a millionare")
End Sub
End Class

Ok I mucked around with it & got it to work. Not sure if its correct but...


firstly VB.net doesnt support Multi Line declarations
With the code you had
Dim Balance, Deposit, intrest As Decimal
you were declaring Balance & Deposit as variants & Interest as Decimal.
Well thats my understanding of it xD
So thats probably why it wasn't calculating right.
Secondly

Balance = ((deposit + balance) *.05) + balance) then

You have 3 closing brackets & only 2 Opening Brackets. The one on the end isn't necessary.

Also numyears wasn't necessary. Changed it to just years.

Well I hope I didn't spoil it to much for you. I tried to explain it :(
 
Status
Not open for further replies.
Back
Top Bottom