简体   繁体   English

我在Visual Basic中遇到了课堂成绩计算器的问题

[英]I'm having trouble in Visual Basic with a grade calculator for class

 Public Class convertGrades
'Here is where I declare my variables
Dim numbergrade As Integer
Dim lettergrade As Char

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'This is an If/Else statement that allows the user to
    'convert their number grade into a letter grade
    txtNumberGrade.Text = numbergrade
    lblLetterGrade.Text = lettergrade

    If (numbergrade >= 90 Or numbergrade <= 100) Then
        lettergrade = "A"
    ElseIf (numbergrade >= 80 Or numbergrade <= 79) Then
        lettergrade = "B"
    ElseIf (numbergrade >= 70 Or numbergrade <= 89) Then
        lettergrade = "C"
    ElseIf (numbergrade >= 60 Or numbergrade <= 69) Then
        lettergrade = "D"
    ElseIf (numbergrade >= 50 Or numbergrade <= 59) Then
        lettergrade = "F"
    End If
    txtNumberGrade.Focus()
End Sub

This is the exact code I have, I am using Visual Basic 2012 and am having trouble converting these numbers into letter grades. 这是我所拥有的确切代码,我正在使用Visual Basic 2012,并且无法将这些数字转换为字母等级。 VB says I have no errors but when I run it, the program does not convert the numbers into letters. VB表示我没有错误,但是运行它时,程序不会将数字转换为字母。 Any suggestions? 有什么建议么?

You swapped the high number for the B and C grades. 您将较高的数字换成B和C等级。 Also, you probably want AndAlso for these checks, rather than Or . 另外,您可能希望AndAlso进行这些检查,而不是Or While I'm here, as a matter of good program design, you should abstract this out to a separate method. 当我在这里时,作为良好的程序设计,您应该将其抽象为一个单独的方法。 The final result: 最终结果:

Public Function LetterGradeFromScore(ByVal score As Integer) As Char
    If numbergrade >= 90 Then
         Return "A"c
    ElseIf numbergrade >= 80 AndAlso numbergrade <= 89 Then
        Return "B"c
    ElseIf numbergrade >= 70 AndAlso numbergrade <= 79 Then
        Return "C"c
    ElseIf numbergrade >= 60 AndAlso numbergrade <= 69 Then
        Return "D"c
    Else
        Return "F"c
    End If
End Function

'The only code that lives in this method is code that directly updates or responds to the UI
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click   
    txtNumberGrade.Text = numbergrade
    lblLetterGrade.Text = LetterGradeFromScore(numbergrade)
    txtNumberGrade.Focus()
End Sub

Another thing: VB.Net has a really nice Select Case syntax you can use for this: 另一件事:VB.Net具有一个非常好的Select Case语法,您可以使用此语法:

Public Function LetterGradeFromScore(ByVal score As Integer) As Char
    Select Case score
        Case >= 90
            Return "A"c
        Case 80 to 89
            Return "B"c
        Case 70 to 79
            Return "C"c
        Case 60 to 69
            Return "D"c
        Case Else
           Return "F"c
    End Select
End Function

Finally, you could avoid a problem like swapping the number above, and as well as improve the code, by converting this to a kind of table lookup: 最后,通过将其转换为一种表查找,可以避免诸如交换上述数字之类的问题,并可以改进代码。

Public Function LetterGradeFromScore(ByVal score As Integer) As Char 

    'This could be a Shared Member somewhere, or even stored in a DB and populated on load
    Dim gradeMap As New SortedList(Of Integer, Char)(5)
    gradeMap.Add(90, "A"c)
    gradeMap.Add(80, "B"c)
    gradeMap.Add(70, "C"c)
    gradeMap.Add(60, "D"c)
    gradeMap.Add( 0, "F"c)

    Return gradeMap.Last(Function(m) m.Key < score).Value
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM