如何处理不正确的格式?



我一直在研究一个程序,可以计算多年来累积复利的房子的首付。用户提供他们节省的金额、年利息金额(百分比)以及累积利息所花费的年数。

我已经设法使用"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

问题 1:

  • 这是有意的,您必须确保不传递无效数据(例如。包含字符的字符串)到函数。你可以试试Double.TryParse,但我只是通过VBA标签来到这里,我对 VB.Net 没有任何经验。
  • 还有其他方法可以验证输入是否为数字。

问题2:

  • 显然(参考您的评论:dubSavings Total = 0 断点处的值)在转换过程中出了点问题,请尝试返回dubSavingsTotal&strSavingsTotal的值,看看转换失败的地方。

示例:
正如我所说,我有 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

最新更新