大家好!需要VB.Net练习的帮助



我正在努力完成这个练习:

修改btDisplay_Click过程,使其使用名为GetGrade101和GetGrade201的两个函数来获得适当的等级;则该过程应在lblGrade控件中显示等级。将这两个独立的Sub过程更改为函数,这些函数为btDisplay_Click过程中调用它们的语句返回适当的级别。每个函数都应该包含一个参数,该参数接受传递给它的总点数

这是我的代码:

Option Explicit On
Option Strict Off
Option Infer Off
Public Class frmMain
Dim intPoints As Integer
Dim Grade As String
Dim GGrade As String

' Independent Sub procedures.
Private Function GetGrade101(ByVal intPoints As Integer) As String
Select Case intPoints
Case Is >= "90"
Grade = "A"
Case Is >= "80"
Grade = "B"
Case Else
Grade = "F"
End Select
Return Grade
End Function
Private Function GetGrade102(ByVal intPoints As Integer) As String
Select Case intPoints
Case Is >= "75"
Grade = "P"
Case Else
Grade = "F"
End Select
Return intPoints
End Function
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' Calls independent Sub procedures to display a grade.

If radHis101.Checked Then
lblGrade.Text = GetGrade101(intPoints)
Else
lblGrade.Text = GetGrade102(intPoints)
End If
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtPoints_Enter(sender As Object, e As EventArgs) Handles txtPoints.Enter
txtPoints.SelectAll()
End Sub
Private Sub ClearGrade(sender As Object, e As EventArgs) Handles txtPoints.TextChanged, radHis101.CheckedChanged, radHis201.CheckedChanged
lblGrade.Text = String.Empty
End Sub
Private Sub txtPoints_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPoints.KeyPress
' Accept only numbers and the Backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
End Class

在两个函数GetGrade1010GetGrade201中,似乎将整数值intpoints视为字符串而不是整数。

将您的SELECT语句更改为类似于以下内容:

选择案例intPointsCase Is>=90'请注意,90左右没有引号,等等。等级="A"情况>=80等级="B"Case ElseGrade="F">

此外,我注意到一个错误。你的一个函数(GetGrade102(不应该是GetGrade201吗?((是不返回你正在计算的分数。更改";CCD_ 7";至";Return Grade";。

现在唯一的问题是它只显示字母f

您将intPoints传递给函数,但从未为其赋值…因此该变量中的当前值为0(ZERO(。你在表单上有一个文本框吗?用户应该在其中输入分数?

是的。假设用户在txtPoints 中输入点数

因此,您需要从txtPoints中提取值,并将其传递给分级函数。然而,TextBox存储一个String,因此您需要首先将其转换为Integer。

一种常见的方法是通过Integer.TryParse((函数:

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' Calls independent Sub procedures to display a grade.
Dim pts As Integer
If Integer.TryParse(txtPoints.Text, pts) Then
If radHis101.Checked Then
lblGrade.Text = GetGrade101(pts).ToString()
Else
lblGrade.Text = GetGrade102(pts).ToString()
End If
Else
MessageBox.Show("Invalid Points!")
End If
End Sub

相关内容

  • 没有找到相关文章

最新更新