为什么我的代码会崩溃? [英] Why does my code keep crashing?
本文介绍了为什么我的代码会崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的一个名为CD Calculator的程序代码不断崩溃;但是,我不知道它有什么问题。
My code for a program called CD Calculator keeps crashing; however, I do not know what is wrong with it.
Public Class Form1
Dim intInitialInvestment As Integer
Dim decInterestRate As Decimal
Dim intValue As Integer
Dim intYears As Integer
Private Sub btnCalculate_Click(sender As Object, e As System.EventArgs) Handles btnCalculate.Click
intInitialInvestment = Val(Me.txtInvestment.Text)
decInterestRate = Val(Me.txtRate.Text)
decInterestRate = decInterestRate / 100
intValue = Val(Me.txtValue.Text)
Dim i As Integer = 1
Do Until intInitialInvestment = intValue
intInitialInvestment = intInitialInvestment + (intInitialInvestment * decInterestRate)
intYears += i
If intInitialInvestment = intValue Then
Me.lblAnswer.Text = "The amount of years are: " & intYears.ToString
End If
Loop
End Sub
End Class
代码块添加,标签更改 - OriginalGriff [/ edit]
[edit]Code block added, Tags changed - OriginalGriff[/edit]
推荐答案
你不要说崩溃时会发生什么,但我猜可能原因是intInitialInvestment
永远不会等于intValue $的值c $ c>,所以你的循环永远不会退出。
首先看看调试器中发生了什么:在线上放一个断点
You don;t say what happens when it crashes, but I'd guess that the likely cause is thatintInitialInvestment
never equals the value ofintValue
, so your loop never exits.
Start by looking at what happens in the debugger: put a breakpoint on the line
Dim i As Integer = 1
并运行你的程序。
当你到达断点时,程序将停止并让你控制正在发生的事情。
一步一步,仔细看看是什么每次都会发生 intInitialInvestment
。
这篇关于为什么我的代码会崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文