1

所以我正在开发一个程序,它会询问作业的数量,比如 20,然后运行一个循环,多次询问每个作业获得的总分以及获得最终成绩的总分。例如,如果用户提交了 2 个作业,作业 1 获得 48 分(满分 50),作业 2 获得 35 分(满分 40),程序将显示成绩为 92。

到目前为止,这是我所拥有的:

Public Class Form1

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub


    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim amountAssignments As Integer
        Dim pointsEarned As Integer = 0
        Dim pointsEarnedTotal As Integer = 0
        Dim pointsPossible As Integer = 0
        Dim pointsPossibleTotal As Integer = 0
        Dim Assignment As Integer = 1

        Integer.TryParse(txtAmount.Text, amountAssignments)

        Do Until Assignment > amountAssignments
            txtAmount.Text = String.Empty
            lblAmount.Text = "Enter Points Given on Assignment " & Assignment & ":"
            Integer.TryParse(txtAmount.Text, pointsEarned)
            pointsEarnedTotal = pointsEarnedTotal + pointsEarned
            lblAmount.Text = "Enter Points Possible on Assignment " & Assignment & ":"
            Integer.TryParse(txtAmount.Text, pointsPossible)
            pointsPossibleTotal = pointsPossibleTotal + pointsPossible
            Assignment = Assignment + 1
        Loop

        lblAmount.Text = "Enter the amount of Assignments: "
        lblGrade.Text = (pointsEarnedTotal / pointsPossibleTotal)

    End Sub
End Class

语法是正确的,但是当程序运行并且输入的作业数量和计算输入时,程序将成绩显示为 NaN,无需其他输入。

可以用另一只眼睛来查看这个并告诉我我在逻辑上搞砸了哪里。

先感谢您!

4

1 回答 1

0

当循环开始时,这个程序似乎没有某种用户干预。我要做的肯定是让用户在文本框中输入分配的数量,然后从那里要求用户从用斜杠分隔的输入框中输入标记,例如我输入 45/50,所以在输入后程序会找到索引的“/”,'/'之前的所有字符都是pointsEarned并且可以添加到pointsEarnedTotal'/'之后的所有字符都被放置pointsPossible并添加到pointsPossibleTotal
尝试这个并告诉我它是怎么回事......给我点如果它工作

` Private Sub butGetMarks_Click(sender As System.Object, e As System.EventArgs) Handles butGetMarks.Click 将分配暗淡为整数 = 0 暗淡 totalAssignments 作为整数 Integer.TryParse(txtAssignments.Text,

    Dim pointsEarned As Double = 0
    Dim pointsEarnedTotal As Double = 0

    Dim possibleEarned As Double = 0
    Dim possibleEarnedTotal As Double = 0
    Dim temp As String

    For i As Integer = 1 To totalAssignments
        temp = InputBox("Enter marks per assignment separated by /")
        Dim i1 As Integer
        i1 = temp.IndexOf("/") 'read index of /
        pointsEarned = temp.Substring(0, i1) 'read from start until / character
        possibleEarned = temp.Substring(i1 + 1) 'read from after / character onwards
        'add to totals
        possibleEarnedTotal += possibleEarned
        pointsEarnedTotal += pointsEarned
    Next i
    MessageBox.Show(pointsEarnedTotal & "/" & possibleEarnedTotal)
End Sub`
于 2013-08-10T20:15:09.360 回答