Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Loops and functions in Visual Basic?
ok, i have a project that im working on and i cant seem to get it to function. the program runs, but instead of displaying the rate like i need it only shows the words. Here is my code. and the instruction for the program. Ive spent 2 weeks working on this and still cant figure it out.
Write a program to solve the following problem: A TV set is purchased with a loan of $563 to be paid off with 5 monthly payments of $116. The interest rate is 1 percent per month. Display a table giving the balance on the loan at the end of each month.
Public Class Form1
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim month As Integer = 1
month = 1
Do Until month = 5 'Repeat loop until month is 5
If month > 5 Then Exit Do ' if month is greater than 5 end the loop cycle
month += 1
DisplayTtl(563)
Dim fmtStr As String = "{0,12} {1, 4:c2}"
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format(fmtStr, "Month", "Rate"))
ListBox1.Items.Add(String.Format(fmtStr, "month", "rate"))
'KNOW this is the problem line, just not sure what it should be instead to make it display the ascending months and descending rate
Loop
'No end if statement needed because no if statement used above
End Sub
Private Function DisplayTtl(ByVal balance As Double) As Double
Dim rate As Double
'Show rate as the balance with interest less monthly amount
rate = (balance * 0.01) - 116
Return rate
End Function
End Class
Answer 1 all i get is a 5 with $110.37 in. the list box
It's better than having just a 0 like I did, but i need the months listed with the amounts so it should end up looking like:
1 (rate)
2 (rate)
3 (rate)
4 (rate)
5 (rate)
ok we have months 2-5 showing. missing month 1. Also the rate is appearing the same in each month instead of decreasing as the months progress :S
ok i fixed the months. :) made it a 0 at top not a 1
ok i fixed the months. :) made it a 0 at top not a 1
all 5 months show but the balance is still the same in each one as opposed to descending from 563 in month one down.
What if we used an array for each of the month results? That way one could reference the next.
Public Class Form1
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim month As Integer = 1
Dim StartValue As Double = 563
month = 1
Dim fmtStr As String = "{0,12} {1, 4:c2}"
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format(fmtStr, "Month", "Rate"))
ListBox1.Items.Add(String.Format(fmtStr, month, DisplayTtl(StartValue)))
Do Until month = 5
If month > 5 Then Exit Do
month += 1
ListBox1.Items.Add(String.Format(fmtStr, month, DisplayTtl(StartValue)))
Loop
End Sub
Private Function DisplayTtl(ByVal balance As Double) As Double
Dim rate As Double
Dim StartValue As Double
rate = (balance * 0.01) - 116
StartValue = rate
Return rate
End Function
End Class
This is what I have now. It still isn't returning the correct rates just $110.37 over and over.
month 1 balance should be 563, then month 2 447 etc until 0 at month 5.
monthly balance = total (563 or lower progression + 0.01) - 116
sorry meant to be a * as in *0.01
So close with that last one, only instead of $563 its putting out $5.63, rate is descending though. I had the same idea using If statements.
everything works as needed except why it is coming as $5.63 instead of $563 as the first balance. after that its perfect :)
making it a 1 seems to make the balance go up instead of down. Weird because I would think that would have made sense.
yes
Public Class Form1
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim month As Integer
Dim balance As Double = 563
Dim rate As Double
month = 1
Dim fmtStr As String = "{0,12} {1, 4:c2}"
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format(fmtStr, "Month", "Rate"))
ListBox1.Items.Add(String.Format(fmtStr, month, DisplayTtl(balance)))
Do Until month = 5
If month > 5 Then Exit Do
month += 1
balance = balance + rate - 116
rate = DisplayTtl(balance)
ListBox1.Items.Add(String.Format(fmtStr, month, rate))
Loop
End Sub
Private Function DisplayTtl(ByVal balance As Double) As Double
Dim rate As Double
rate = (balance * 1.01) - 116
Return rate
End Function
End Class
everything is great guys! thank you. I have a successful running program! YAY!!!!
3 Answers
- 1 decade agoFavorite Answer
Try this code out:
======Current Code Edit
Public Class Form1
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim month As Integer
Dim balance As Double = 563
Dim rate As Double
month = 1
Dim fmtStr As String = "{0,12} {1, 4:c2}"
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format(fmtStr, "Month", "Rate"))
ListBox1.Items.Add(String.Format(fmtStr, month, DisplayTtl(balance)))
Do Until month = 5
If month > 5 Then Exit Do
month += 1
balance = DisplayTtl(balance)
ListBox1.Items.Add(String.Format(fmtStr, month, balance))
Loop
End Sub
Private Function DisplayTtl(ByVal balance As Double) As Double
Dim rate As Double
rate = (balance * 1.01) - 116
Return rate
End Function
End Class
=======End of current code edit
Instead of making month = 0, I would recommend keeping it as month = 1 and moving month += 1 just above the line 'Loop'. With month +=1 at the top of the loop statement, you are adding 1 to month before doing anything with it. It is better to add 1 to month at the end of the loop statement.
Also, your function call DisplayTtl(563) will always return the same value because it is being fed the same number (563) each time it is called. Instead, create a variable for the starting value and assign it the value 563:
Dim StartValue As Double = 563
Then use that variable in the function call:
ListBox1.Items.Add(String.Format (fmtStr, month, DisplayTtl(StartValue) ))
Then make sure to assign the new value to StartValue in the DisplayTtl function:
Private Function DisplayTtl(ByVal balance As Double) As Double
Dim rate As Double
rate = (balance * 0.01) - 116
StartValue = rate
Return rate
End Function
End Class
For the last issue:
change
rate=(balance * 0.01) - 116
to:
rate = (balance * 1.01) - 116
Reason:
rate = (current balance + interest on balance) - payment
and
interest on balance = current balance * .01 so
rate = (current balance + currentbalance * .01) - payment
rate = (current balance * (1 + .01)) - payment
rate = (current balance * 1.01) - payment
Is the -116 still in the rate equation?
Hooray! Have a good night! :)
- Anonymous1 decade ago
Try this code, I've modified some parts of your code:
Public Class Form1
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim month As Integer
Dim balance As Double
Dim rate As Double
balance = 563
month = 1
'table initialization
Dim fmtStr As String = "{0,12} {1, 4:c2}"
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format (fmtStr, "Month", "Rate") )
ListBox1.Items.Add(String.Format (fmtStr, month, balance))
Do Until month = 5
If month > 5 Then Exit Do
month += 1
rate = DisplayTtl(balance)
balance = balance + rate - 116
ListBox1.Items.Add(String.Format (fmtStr, month, balance))
Loop
End Sub
Private Function DisplayTtl(ByVal balance As Double) As Double
Dim rate As Double
rate = (balance * 0.01)
Return rate
End Function
End Class
I think this should solve your problem.
See if it works. If it doesn't, please let me know.
Hope this helps :)
---
- Anonymous5 years ago
attempt this code, i have changed some elements of your code: Public classification Form1 deepest Sub btnDisplay_Click(ByVal sender As equipment.merchandise, ByVal e As equipment.EventArgs) Handles btnDisplay.click Dim month As Integer Dim stability As Double Dim fee As Double stability = 563 month = a million 'table initialization Dim fmtStr As String = "{0,12} {a million, 4:c2}" ListBox1.products.clean() ListBox1.products.upload(String.format (fmtStr, "Month", "fee") ) ListBox1.products.upload(String.format (fmtStr, month, stability)) Do until eventually month = 5 If month > 5 Then go out Do month += a million fee = DisplayTtl(stability) stability = stability + fee - 116 ListBox1.products.upload(String.format (fmtStr, month, stability)) Loop end Sub deepest function DisplayTtl(ByVal stability As Double) As Double Dim fee As Double fee = (stability * 0.01) go back fee end function end classification i imagine this may clean up your problem. See if it particularly works. If it would not, please enable me recognize. wish this facilitates :) ---