多年来,我一直在研究一个程序,该程序计算房屋的首付以及复利的累积。用户提供他们节省的金额,年利率(百分比)以及累积利息所花费的年数。
我已经设法使用“ for”循环来弄清楚其逻辑。我的作业指出,我也不能接受在输入框中输入的负数或字母。这就是我遇到问题的地方。
问题1:如果在“储蓄”输入框对象中输入了字母,程序将崩溃并抛出以下错误代码:“输入字符串的格式不正确” ...这是在引用以下代码行:
dubSavingsTotal = Convert.ToDouble(strSavingsTotal)
问题2:在“储蓄”输入框中输入负数时,程序似乎忽略了以下if语句
If dubSavingsTotal < 0 Then
strSavingsTotal = InputBox(strErrorMessageSavings, , "")
End If
请原谅我,但似乎最简单的方法是使用if / else语句,该语句指出,如果给定值小于0或不是数字,则将出现一个消息框,并且用户会提示您在输入框中输入另一个数字。
这是我在下面的代码中尝试过的操作,我拥有if语句旨在阻止用户在储蓄输入框中输入字母或负数。谢谢大家提供的所有帮助,非常感谢。
Option Strict On
Public Class frmhomedownpayment
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'This Code is executed when the user clicks on the "Enter Savings Data" button
'The user then enters the savings, interest and year values into an input box
'The data is then displayed to the right of the application window, while the interest
'amounts are calculated and displayed in the listbox object.
'Declare Variables
Dim dubSavingsTotal As Double
Dim dubInterestTotal As Double
Dim dubYearsTotal As Double
Dim dublistTotal As Double
Dim strSavingsTotal As String
Dim strInterestTotal As String
Dim strYearsTotal As String
Dim strSavingsTotalMessage As String = "Enter your current savings"
Dim strInterestTotalMessage As String = "Enter the annual interest amount."
Dim strInterestTotalMessage2 As String = "The number should be entered as a percent (5 for 5%)"
Dim strYearsTotalMessage As String = "Enter the Years"
Dim strErrorMessageYears As String = "Data Error (Years) : Enter a Numeric Value greater than 0!"
Dim strErrorMessageSavings As String = " Data Error (Savings) : Enter a Numeric Value greater than 0!"
Dim strErrorMessageInterest As String = " Data Error (Interest) : Enter a Numeric Value greater than 0!"
'Input Boxes
strSavingsTotal = InputBox(strSavingsTotalMessage, " ")
strInterestTotal = InputBox(strInterestTotalMessage & strInterestTotalMessage2, " ")
strYearsTotal = InputBox(strYearsTotalMessage, " ")
'Error Catching
If Not IsNumeric(dubSavingsTotal) Then
strSavingsTotal = InputBox(strErrorMessageSavings, , "")
End If
If dubSavingsTotal < 0 Then
strSavingsTotal = InputBox(strErrorMessageSavings, , "")
End If
'Conversions to Double
dubSavingsTotal = Convert.ToDouble(strSavingsTotal)
dubInterestTotal = Convert.ToDouble(strInterestTotal)
dubYearsTotal = Convert.ToDouble(strYearsTotal)
'We want the entered number to be expressed as a percentage
dubInterestTotal = dubInterestTotal / 100
'Display the Data
lblInterestTotal.Text = dubInterestTotal.ToString("P")
lblYearTotal.Text = strYearsTotal.ToString
lblSavingsTotal.Text = dubSavingsTotal.ToString("C")
'A 'For' loop that calculates the interest amounts and displays them
to a listbox object
***starting from the first year***
For dubYearsTotal = 1 To dubYearsTotal
dublistTotal = ((dubInterestTotal * dubSavingsTotal) *
dubYearsTotal)
lstTotals.Items.Add(dublistTotal + dubSavingsTotal)
Next
End Sub
答案 0 :(得分:0)
问题1:
问题2:
dubSavingsTotal
和strSavingsTotal
的值并查看转换失败的地方。也许改为尝试Double.Parse。 示例:
就像我说的那样,我有使用VB.Net的经验,没有办法测试此代码,但是它应该在理论上起作用。来源:MS Docs,Quora,Dream In Code
dim someString as String
dim someDouble as Double
if Double.TryParse(someString, someDouble) then
Console.Write("Conversion successful, converted " & someString & " to " & someDouble)
else if
Console.Write("Conversion failed, " & someString & " could not be converted")
end if