我应该为一项足球比赛的编程作业制作一个计分器。 它有4个文本框和一个按钮,功能需要完全信用,我只是不确定我做错了什么。
Public Class Form1
Dim intTotal = 0
Dim intFirst = 0
Dim intSecond = 0
Dim intThird = 0
Dim intFourth = 0
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Try
Dim intFirst As Integer = Convert.ToInt32(txtFirst.Text)
Dim intSecond As Integer = Convert.ToInt32(txtSecond.Text)
Dim intThird As Integer = Convert.ToInt32(txtThird.Text)
Dim intFourth As Integer = Convert.ToInt32(txtFourth.Text)
Catch ex As Exception
MessageBox.Show("Enter in Digits!")
End Try
intTotal = calcTotal(intFirst, intSecond, intThird, intFourth, intTotal)
Me.lblTotal.Text = intTotal 'Shows as 0 at run-time
End Sub
Function calcTotal(ByVal intFirst As Integer, ByVal intSecond As Integer, ByVal intThird As Integer, ByVal intFourth As Integer, ByVal intTotal As Integer) As Integer
intTotal = intFirst + intSecond + intThird + intFourth
Return intTotal
End Function
End Class
lblTotal
最终显示0。
答案 0 :(得分:2)
您的变量在try catch内的块级声明。将声明移出块并删除类级声明(因为它们并非必要)。
像这样:
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim intFirst As Integer = 0
Dim intSecond As Integer = 0
Dim intThird As Integer = 0
Dim intFourth As Integer = 0
Try
intFirst = Convert.ToInt32(txtFirst.Text)
intSecond = Convert.ToInt32(txtSecond.Text)
intThird = Convert.ToInt32(txtThird.Text)
intFourth = Convert.ToInt32(txtFourth.Text)
Catch ex As Exception
MessageBox.Show("Enter in Digits!")
End Try
Dim intTotal as Integer = calcTotal(intFirst, intSecond, intThird, intFourth, intTotal)
Me.lblTotal.Text = intTotal 'Shows as 0 at run-time
End Sub
Function calcTotal(ByVal intFirst As Integer, ByVal intSecond As Integer, ByVal intThird As Integer, ByVal intFourth As Integer, ByVal intTotal As Integer) As Integer
Return intFirst + intSecond + intThird + intFourth
End Function
End Class
答案 1 :(得分:0)
我不确定你的问题是什么,但你的变量声明不属于你的功能。你应该在里面初始化它们。