VB.Net:成绩计算器计划 - 获取可笑的价值观

时间:2012-09-27 04:44:22

标签: vb.net

我正在尝试创建一个成绩计算器程序,我遇到了两个问题:

  1. 获得正确的输出(因为我得到了荒谬的数字)和
  2. 让我的计数器工作。
  3. 基本形式基本上是一个输入7个输入等级:

    • 3次考试(分别称重15%,20%和20%)
    • 一个项目(权重10%),
    • 作业(加权20%),
    • 同行评审(加权5%),
    • 编程语言演示文稿(加权10%)

    并且个人应该获得他们的数字等级,他们的字母等级和两个计数器的输出,这些计数器计算有多少人得到A'sF's

    例如,当我输入3个考试成绩时:828791; Assignments: 94; Peer reviews: 100; programming language presentation: 90;final project: 92,

    我的最终数字成绩为253.90,字母成绩为F,但显然应该是A和数字89.90的字母成绩。

    我的计数器也无法正常工作,因为它们没有显示count,但我觉得我把它放在正确的位置(显示输出)。我到底错在了什么?这是我的代码

    Option Strict On
    
    Public Class frmGradeCalculator
        'declare Constants
        Const EXAM1GRADE_WEIGHT As Decimal = 0.15D
        Const EXAM2GRADE_WEIGHT As Decimal = 0.2D
        Const EXAM3GRADE_WEIGHT As Decimal = 0.2D
        Const HOMEWORKGRADE_WEIGHT As Decimal = 0.2D
        Const HOMEWORKPEERREVIEW_WEIGHT As Decimal = 0.05D
        Const LANGUAGEQUICKREFERENCE_WEIGHT As Decimal = 0.1D
        Const FINALPROJECT_WEIGHT As Decimal = 0.1D
    
        'Declare module variables
        Dim mdecFinalNumericGrade As Decimal
        Dim mstrFinalLetterGrade As String
        Dim mintStudentsWithAs As Integer
        Dim mintStudentsWithFs As Integer
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    
            'declare variables
            Dim decExam1Grade As Decimal
            Dim decExam2Grade As Decimal
            Dim decExam3Grade As Decimal
            Dim decHomeworkGrade As Decimal
            Dim decPeerReviewGrade As Decimal
            Dim decLanguageReferenceGrade As Decimal
            Dim decFinalProjectGrade As Decimal
            Dim decPercent As Decimal
    
            'check for blanks
            If (txtExam1Grade.Text) = "" Then
                MessageBox.Show("You Can't Leave Exam 1 Blank")
                Exit Sub
            End If
    
            'check for numeric
            If IsNumeric(txtExam1Grade.Text) = False Then 'value is not numeric
                MessageBox.Show("Please enter a numeric value for Exam 1!")
                Exit Sub
            End If
    
            'check for blanks
            If (txtExam2Grade.Text) = "" Then
                MessageBox.Show("Please enter Exam 2!")
                Exit Sub
            End If
            'check for everything else
            If IsNumeric(txtExam2Grade.Text) = False Then 'Value is not numeric
                MessageBox.Show("Please enter a numeric value for Exam 2!")
                Exit Sub
            End If
    
            'check for blanks
            If (txtExam3Grade.Text) = "" Then
                MessageBox.Show("Please enter Exam 3!")
                Exit Sub
            End If
    
            'check for numerics
            If IsNumeric(txtExam3Grade.Text) = False Then
                MessageBox.Show("Please enter a positive numeric value for Exam3!")
                Exit Sub
            End If
    
            'check for blanks
            If (txtHomeworkGrade.Text) = "" Then
                MessageBox.Show("Please enter Homework Grade!")
                Exit Sub
            End If
    
            'check for numerics
            If IsNumeric(txtHomeworkGrade.Text) = False Then
                MessageBox.Show("Please enter a numeric positive value for Homework Grade!")
                Exit Sub
            End If
    
            'check for blanks
            If (txtPeerReviewGrade.Text) = "" Then
                MessageBox.Show("Please enter a Peer Review Grade!")
                Exit Sub
            End If
    
            'check for numerics
            If IsNumeric(txtPeerReviewGrade.Text) = False Then
                MessageBox.Show("Please enter a numeric positive value for Peer Review Grade!")
                Exit Sub
            End If
    
            'check for blanks
            If (txtLanguageReferenceGrade.Text) = "" Then
                MessageBox.Show("Please enter a Language Reference Grade!")
                Exit Sub
            End If
    
            'check for numerics
            If IsNumeric(txtLanguageReferenceGrade.Text) = False Then
                MessageBox.Show("Please enter a numeric positive value for Language Reference Grade!")
                Exit Sub
            End If
            'check for blanks
            If (txtFinalProjectGrade.Text) = "" Then
                MessageBox.Show("Please enter a Final Project Grade!")
                Exit Sub
            End If
    
            'check for numerics
            If IsNumeric(txtFinalProjectGrade.Text) = False Then
                MessageBox.Show("Please enter a numeric positive value for Final Project Grade!")
                Exit Sub
            End If
    
            'convert data types
            decExam1Grade = Convert.ToDecimal(txtExam1Grade.Text)
            decExam2Grade = Convert.ToDecimal(txtExam2Grade.Text)
            decExam3Grade = Convert.ToDecimal(txtExam3Grade.Text)
            decHomeworkGrade = Convert.ToDecimal(txtHomeworkGrade.Text)
            decPeerReviewGrade = Convert.ToDecimal(txtPeerReviewGrade.Text)
            decLanguageReferenceGrade = Convert.ToDecimal(txtLanguageReferenceGrade.Text)
            decFinalProjectGrade = Convert.ToDecimal(txtFinalProjectGrade.Text)
            mdecFinalNumericGrade = (decExam1Grade * EXAM1GRADE_WEIGHT) + _
                                    (decExam2Grade * EXAM2GRADE_WEIGHT) + _
                                    (decExam3Grade * EXAM3GRADE_WEIGHT) + _
                                    (decHomeworkGrade * HOMEWORKGRADE_WEIGHT) + _
                                    (decPeerReviewGrade * HOMEWORKPEERREVIEW_WEIGHT) + _
                                    (decLanguageReferenceGrade + LANGUAGEQUICKREFERENCE_WEIGHT) + _
                                    (decFinalProjectGrade + FINALPROJECT_WEIGHT)
    
            'check for 0 or positive
            If decExam1Grade < 0 Then
                MessageBox.Show("Please enter a positive value or zero for Exam 1!")
                Exit Sub
            End If
    
            'check for 0 or positive
            If decExam2Grade < 0 Then
                MessageBox.Show("Please enter a positive value or zero for Exam 2!")
                Exit Sub
            End If
    
            'check for 0 or positive
            If decExam3Grade < 0 Then
                MessageBox.Show("Please enter a positive value or zero for Exam 3!")
                Exit Sub
            End If
    
            'check for 0 or positive
            If decHomeworkGrade < 0 Then
                MessageBox.Show("Please enter a positive value or zero for Homework Grade!")
                Exit Sub
            End If
    
            'check for 0 or positive
            If decPeerReviewGrade < 0 Then
                MessageBox.Show("Please enter a positive value or zero for Peer Review Grade!")
                Exit Sub
            End If
    
            'check for 0 or positive
            If decLanguageReferenceGrade < 0 Then
                MessageBox.Show("Please enter a positive value or zero for Language Reference!")
                Exit Sub
            End If
    
            'check for 0 or positive
            If decFinalProjectGrade < 0 Then
                MessageBox.Show("Please enter a positive value or zero for Final Project Grade!")
                Exit Sub
            End If
    
            'make sure values are less than 100
            If decExam1Grade > 100 Then
                MessageBox.Show("Please enter a value thats 100 or less!")
            End If
    
            If decExam2Grade > 100 Then
                MessageBox.Show("Please enter a value thats 100 or less!")
            End If
    
            If decExam3Grade > 100 Then
                MessageBox.Show("Please enter a value thats 100 or less!")
            End If
    
            If decHomeworkGrade > 100 Then
                MessageBox.Show("Please enter a value thats 100 or less!")
            End If
    
            If decPeerReviewGrade > 100 Then
                MessageBox.Show("Please enter a value thats 100 or less!")
            End If
    
            If decLanguageReferenceGrade > 100 Then
                MessageBox.Show("Please enter a value thats 100 or less!")
            End If
    
            If decFinalProjectGrade > 100 Then
                MessageBox.Show("Please enter a value thats 100 or less!")
            End If
    
            'Determine grade per letter
            Select Case decpercent
                Case Is >= 89.5D
                    mstrFinalLetterGrade = "A"
                    mintStudentsWithAs += 1
                Case Is >= 79.5D
                    mstrFinalLetterGrade = "B"
                Case Is >= 69.5D
                    mstrFinalLetterGrade = "C"
                Case Is >= 59.5D
                    mstrFinalLetterGrade = "D"
                Case Else
                    mstrFinalLetterGrade = "F"
                    mintStudentsWithFs += 1
            End Select
    
            lblFinalLetterGrade.Text = mstrFinalLetterGrade
    
            'display outputs
            lblFinalNumericGrade.Text = mdecFinalNumericGrade.ToString("f2")
        End Sub
    
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
            'clear the texboxes and labels
            txtExam1Grade.Clear()
            txtExam2Grade.Clear()
            txtExam3Grade.Clear()
            txtHomeworkGrade.Clear()
            txtPeerReviewGrade.Clear()
            txtLanguageReferenceGrade.Clear()
            txtFinalProjectGrade.Clear()
            lblFinalLetterGrade.Text = ""
            lblFinalNumericGrade.Text = ""
    
            'setcursor back to top textbox
            txtExam1Grade.Focus()
    
        End Sub
    
        Private Sub btnReset_Click(ByVal sende As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    
            'reset module level variables
            mdecFinalNumericGrade = 0
            mstrFinalLetterGrade = ""
            mintStudentsWithAs = 0
            mintStudentsWithFs = 0
    
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
            'close the form
            End
        End Sub
    
        Private Sub frmGradeCalculator_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    

1 个答案:

答案 0 :(得分:1)

您的最终成绩总数是错误的,因为您没有将语言表达和最终项目乘以其权重,而是将权重加到得分上:

  • Exam1 = 82 * .15 = 12.3
  • Exam2 = 87 * .20 = 17.4
  • Exam3 = 91 * .20 = 18.2
  • 作业= 94 * .20 = 18.8
  • 同行评审= 100 * .05 = 5
  • 语言表达= 90 + .10 = 90.1
  • 最终项目= 92 + .10 = 92.1

注意最后两个值的加法(而不是乘法)。总数是253.9。

将medcFinalNumericGrade的最后两个计算更改为:

(decLanguageReferenceGrade * LANGUAGEQUICKREFERENCE_WEIGHT) + _
(decFinalProjectGrade * FINALPROJECT_WEIGHT)

您将始终获得“F”,因为永远不会为decpercent分配值,因此会执行Else Case。指定decpercent值,或使用mdecFinalNumericGrade。

例如:

Select Case mdecFinalNumericGrade

而不是

Select Case decpercent

修复Select Case后,您的计数应该有效(除非您重置表单)。