混淆了VB.Net中的错误代码

时间:2016-01-10 22:55:54

标签: vb.net

我的代码产生错误“类型'Error.Module1.student'的错误无法转换为'Average.Module1.student的1维数组'。”在第15行。我在俯瞰什么?

模块Module1 进口系统

Class student
    Public id As Integer
    Public name As String
    Public M1 As Integer
    Public M2 As Integer
    Public objStudent As String
    Public Function average() As Double
        Return (M1 + M2) / 2
    End Function
End Class
Class Program
    Private Shared Sub Main(args As String())
        Dim objstudent() As student = New student()

        Dim s As student
        For i As Integer = 0 To 14
            s = New student()
            Console.WriteLine("Enter the Student id")
            s.id = Convert.ToInt32(Console.ReadLine())
            Console.WriteLine("Enter the Student Name")
            s.name = Console.ReadLine()
            Console.WriteLine("Enter the first mark")
            s.M1 = Convert.ToInt32(Console.ReadLine())
            Console.WriteLine("Enter the second mark")
            s.M2 = Convert.ToInt32(Console.ReadLine())
            objstudent(i) = s
        Next
        For Each ss As student In objstudent
            Dim a As Double = ss.average()
            If a >= 90 Then
                Console.WriteLine(ss.name + "Your grade is A")
            ElseIf (a >= 80) AndAlso (a < 90) Then
                Console.WriteLine(ss.name + "Your grade is B")
            ElseIf (a >= 70) AndAlso (a < 80) Then
                Console.WriteLine(ss.name + "Your grade is C")
            ElseIf (a >= 60) AndAlso (a < 70) Then
                Console.WriteLine(ss.name + "Your grade is D")
            Else
                Console.WriteLine(ss.name + "Sorry, You failed")
            End If

     Next

    End Sub
End Class

结束模块

1 个答案:

答案 0 :(得分:0)

Dim objstudent() As student = New student()

这是不正确的数组声明。您声明您想要一个学生数组但是您使用Student构造函数。所以你实际告诉:

new array = new object

这是正确的声明。请注意,数组是从0开始的。所以:

Dim objstudent As student() = New student(14) {}

是一个由15名学生组成的阵列。

Class student
    Public id As Integer
    Public name As String
    Public M1 As Integer
    Public M2 As Integer
    Public objStudent As String
    Public Function average() As Double
        Return (M1 + M2) / 2
    End Function
End Class
Class Program
    Private Shared Sub Main(args As String())
        Dim objstudent As student() = New student(14) {}

        Dim s As student
        For i As Integer = 0 To 14
            s = New student()
            Console.WriteLine("Enter the Student id")
            s.id = Convert.ToInt32(Console.ReadLine())
            Console.WriteLine("Enter the Student Name")
            s.name = Console.ReadLine()
            Console.WriteLine("Enter the first mark")
            s.M1 = Convert.ToInt32(Console.ReadLine())
            Console.WriteLine("Enter the second mark")
            s.M2 = Convert.ToInt32(Console.ReadLine())
            objstudent(i) = s
        Next
        For Each ss As student In objstudent
            Dim a As Double = ss.average()
            If a >= 90 Then
                Console.WriteLine(ss.name + "Your grade is A")
            ElseIf (a >= 80) AndAlso (a < 90) Then
                Console.WriteLine(ss.name + "Your grade is B")
            ElseIf (a >= 70) AndAlso (a < 80) Then
                Console.WriteLine(ss.name + "Your grade is C")
            ElseIf (a >= 60) AndAlso (a < 70) Then
                Console.WriteLine(ss.name + "Your grade is D")
            Else
                Console.WriteLine(ss.name + "Sorry, You failed")
            End If

     Next

    End Sub
End Class