Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用字符串和整数对数组进行数字排序_Arrays_Vb.net_Sorting - Fatal编程技术网

Arrays 使用字符串和整数对数组进行数字排序

Arrays 使用字符串和整数对数组进行数字排序,arrays,vb.net,sorting,Arrays,Vb.net,Sorting,我有一个数组,它旁边有一个名字和一个分数,我需要它从高到低排序。我已经按字母顺序把它分类了 Dim reply3 As String Dim name As String Dim score As Integer = 0 Dim classnum As Integer Dim filePath As String Dim reply As Integer Dim reply2 As Integer Dim strline As String Dim array() As String Sub

我有一个数组,它旁边有一个名字和一个分数,我需要它从高到低排序。我已经按字母顺序把它分类了

Dim reply3 As String
Dim name As String
Dim score As Integer = 0
Dim classnum As Integer
Dim filePath As String
Dim reply As Integer
Dim reply2 As Integer
Dim strline As String
Dim array() As String

Sub Main()

    Console.Title = "Math Test"
    Console.WriteLine("Do you want to start the test or view previous results? Press 1 for test, 2 for results")

    reply = Console.ReadLine

    If reply > 2 Then
        MsgBox("Invalid Reply Press Ok to end")
        End
    End If

    If reply = 2 Then
        Console.WriteLine("What class do you want to see the results of? 1, 2, or 3?")
        reply2 = Console.ReadLine()
    End If

    If reply2 > 3 Then
        MsgBox("Invalid Reply Press Ok to exit")
        End
    End If

    Select Case reply2
        Case 1

            Dim results1 As String = File.ReadAllText("Z:\scores class 1.txt")

            array = Split(results1, "-")

            For i As Integer = 0 To array.Length - 1
                Console.WriteLine(array(i))
            Next

            Console.WriteLine("Would you like these to be sorted? Press 1 for yes, 2 for no")
            If Console.ReadLine = 1 Then

                System.Array.Sort(array)

                For i As Integer = 0 To array.Length - 1
                    Console.WriteLine(array(i))
                Next


            ElseIf Console.ReadLine = 2 Then
                End
            End If

            Console.ReadLine()

        Case 2

            Dim results1 As String = File.ReadAllText("Z:\scores class 2.txt")

            array = Split(results1, "-")

            For i As Integer = 0 To array.Length - 1
                Console.WriteLine(array(i))
            Next

            Console.WriteLine("Would you like these to be sorted? Press 1 for yes, 2 for no")
            If Console.ReadLine = 1 Then

                System.Array.Sort(array)

                For i As Integer = 0 To array.Length - 1
                    Console.WriteLine(array(i))
                Next

            ElseIf Console.ReadLine = 2 Then
                End
            End If
            Console.ReadLine()

        Case 3

            Dim results1 As String = File.ReadAllText("Z:\scores class 3.txt")

            array = Split(results1, "-")

            For i As Integer = 0 To array.Length - 1
                Console.WriteLine(array(i))
            Next

            Console.WriteLine("Would you like these to be sorted? Press 1 for yes, 2 for no")
            If Console.ReadLine = 1 Then

                System.Array.Sort(array)

                For i As Integer = 0 To array.Length - 1
                    Console.WriteLine(array(i))
                Next

            ElseIf Console.ReadLine = 2 Then
                End
            End If
            Console.ReadLine()

    End Select

    If reply = 1 Then

        Console.WriteLine("What is your name?")
        name = Console.ReadLine

        Console.WriteLine("What class are you in, 1, 2 or 3?")
        classnum = Console.ReadLine

        If classnum < 1 Then
            MsgBox("Invalid Class number")
            End
        ElseIf classnum > 3 Then
            MsgBox("Invalid Class number")
            End
        End If

        Console.WriteLine("What is 9+10 ?")
        If Console.ReadLine = 19 Then
            score += 1
        End If

        Console.WriteLine("What is 5x10 ?")
        If Console.ReadLine = 50 Then
            score += 1
        End If

        Console.WriteLine("What is 122÷2 ?")
        If Console.ReadLine = 61 Then
            score += 1
        End If

        Console.WriteLine("What is 424 + 10 ?")
        If Console.ReadLine = 434 Then
            score += 1
        End If

        Console.WriteLine("What is 234 x 3 ?")
        If Console.ReadLine = 702 Then
            score += 1
        End If

        Console.WriteLine("What is 10 x 10 ?")
        If Console.ReadLine = 100 Then
            score += 1
        End If

        Console.WriteLine("What is 12 x 64 ?")
        If Console.ReadLine = 768 Then
            score += 1
        End If

        Console.WriteLine("What is the value of N in this equation? 2n+6=10?")
        If Console.ReadLine = 4 Then
            score += 1
        End If

        Console.WriteLine("What is 9 x 73 ?")
        If Console.ReadLine = 657 Then
            score += 1
        End If

        Console.WriteLine("What is 1 + 1 ?")
        If Console.ReadLine = 2 Then
            score += 1

        End If

        MsgBox("Your score was " & score & " Click ok to finish.")

        Dim output1 As String = name & " " & score & "-"

        Select Case classnum
            Case 1
                filePath = System.IO.Path.Combine(
           My.Computer.FileSystem.SpecialDirectories.MyDocuments, "scores class 1.txt")
                My.Computer.FileSystem.WriteAllText(filePath, output1, True)
            Case 2
                filePath = System.IO.Path.Combine(
            My.Computer.FileSystem.SpecialDirectories.MyDocuments, "scores class 2.txt")
                My.Computer.FileSystem.WriteAllText(filePath, output1, True)
            Case 3
                filePath = System.IO.Path.Combine(
            My.Computer.FileSystem.SpecialDirectories.MyDocuments, "scores class 3.txt")
                My.Computer.FileSystem.WriteAllText(filePath, output1, True)
        End Select

    End If

End Sub
Dim reply3作为字符串
将名称设置为字符串
将分数设置为整数=0
Dim classnum作为整数
将文件路径设置为字符串
作为整数的Dim应答
Dim reply2为整数
暗斯特林作为弦
Dim array()作为字符串
副标题()
Console.Title=“数学测试”
Console.WriteLine(“您想开始测试还是查看以前的结果?按1表示测试,按2表示结果”)
reply=Console.ReadLine
如果回复>2,则
MsgBox(“无效回复,按Ok结束”)
终点
如果结束
如果回答=2,则
WriteLine(“您希望看到哪个类的结果?1、2或3?”)
reply2=Console.ReadLine()
如果结束
如果回复2>3,则
MsgBox(“无效回复,按Ok退出”)
终点
如果结束
选择案例回复2
案例1
Dim results1作为字符串=File.ReadAllText(“Z:\scores class 1.txt”)
数组=拆分(结果1,“-”)
对于i,整数=0到数组。长度-1
控制台写入线(数组(i))
下一个
Console.WriteLine(“是否要对这些进行排序?按1表示是,按2表示否”)
如果Console.ReadLine=1,则
System.Array.Sort(数组)
对于i,整数=0到数组。长度-1
控制台写入线(数组(i))
下一个
ElseIf Console.ReadLine=2然后
终点
如果结束
Console.ReadLine()
案例2
Dim results1作为字符串=File.ReadAllText(“Z:\scores class 2.txt”)
数组=拆分(结果1,“-”)
对于i,整数=0到数组。长度-1
控制台写入线(数组(i))
下一个
Console.WriteLine(“是否要对这些进行排序?按1表示是,按2表示否”)
如果Console.ReadLine=1,则
System.Array.Sort(数组)
对于i,整数=0到数组。长度-1
控制台写入线(数组(i))
下一个
ElseIf Console.ReadLine=2然后
终点
如果结束
Console.ReadLine()
案例3
Dim results1作为字符串=File.ReadAllText(“Z:\scores class 3.txt”)
数组=拆分(结果1,“-”)
对于i,整数=0到数组。长度-1
控制台写入线(数组(i))
下一个
Console.WriteLine(“是否要对这些进行排序?按1表示是,按2表示否”)
如果Console.ReadLine=1,则
System.Array.Sort(数组)
对于i,整数=0到数组。长度-1
控制台写入线(数组(i))
下一个
ElseIf Console.ReadLine=2然后
终点
如果结束
Console.ReadLine()
结束选择
如果reply=1,则
Console.WriteLine(“你叫什么名字?”)
name=Console.ReadLine
Console.WriteLine(“您在哪一类,1、2或3?”)
classnum=Console.ReadLine
如果classnum<1,则
MsgBox(“无效的类号”)
终点
如果classnum>3,则
MsgBox(“无效的类号”)
终点
如果结束
Console.WriteLine(“9+10是什么?”)
如果Console.ReadLine=19,则
分数+=1
如果结束
Console.WriteLine(“什么是5x10?”)
如果Console.ReadLine=50,则
分数+=1
如果结束
Console.WriteLine(“什么是122÷2?”)
如果Console.ReadLine=61,则
分数+=1
如果结束
Console.WriteLine(“什么是424+10?”)
如果Console.ReadLine=434,则
分数+=1
如果结束
Console.WriteLine(“什么是234 x 3?”)
如果Console.ReadLine=702,则
分数+=1
如果结束
Console.WriteLine(“什么是10 x 10?”)
如果Console.ReadLine=100,则
分数+=1
如果结束
Console.WriteLine(“什么是12 x 64?”)
如果Console.ReadLine=768,则
分数+=1
如果结束
WriteLine(“这个等式中N的值是多少?2n+6=10?”)
如果Console.ReadLine=4,则
分数+=1
如果结束
Console.WriteLine(“什么是9 x 73?”)
如果Console.ReadLine=657,则
分数+=1
如果结束
Console.WriteLine(“1+1是什么?”)
如果Console.ReadLine=2,则
分数+=1
如果结束
MsgBox(“您的分数为”&分数&“单击确定以完成”。)
Dim output1作为字符串=名称和分数
选择Case classnum
案例1
filePath=System.IO.Path.Combine(
My.Computer.FileSystem.SpecialDirectory.MyDocuments,“分数等级1.txt”)
My.Computer.FileSystem.WriteAllText(文件路径,output1,True)
案例2
filePath=System.IO.Path.Combine(
My.Computer.FileSystem.SpecialDirectory.MyDocuments,“分数等级2.txt”)
My.Computer.FileSystem.WriteAllText(文件路径,output1,True)
案例3
filePath=System.IO.Path.Combine(
My.Computer.FileSystem.SpecialDirectory.MyDocuments,“分数等级3.txt”)
My.Computer.FileSystem.WriteAllText(文件路径,output1,True)
结束选择
如果结束
端接头

我需要对数组进行数字排序。我将添加一个选项,供用户选择在按字母顺序排序后是否需要进一步排序。

您拥有的基本上是一个包含多个数据类型的多维数组(但在您的示例中,您已将每一行连接到一个字符串中)。处理这种情况没有一个通盘的办法,但这里有一些方法可以考虑:

Dim reply3 As String
Dim name As String
Dim score As Integer = 0
Dim classnum As Integer
Dim filePath As String
Dim reply As Integer
Dim reply2 As Integer
Dim strline As String
Dim array() As String

Sub Main()

    Console.Title = "Math Test"
    Console.WriteLine("Do you want to start the test or view previous results? Press 1 for test, 2 for results")

    reply = Console.ReadLine

    If reply > 2 Then
        MsgBox("Invalid Reply Press Ok to end")
        End
    End If

    If reply = 2 Then
        Console.WriteLine("What class do you want to see the results of? 1, 2, or 3?")
        reply2 = Console.ReadLine()
    End If

    If reply2 > 3 Then
        MsgBox("Invalid Reply Press Ok to exit")
        End
    End If

    Select Case reply2
        Case 1

            Dim results1 As String = File.ReadAllText("Z:\scores class 1.txt")

            array = Split(results1, "-")

            For i As Integer = 0 To array.Length - 1
                Console.WriteLine(array(i))
            Next

            Console.WriteLine("Would you like these to be sorted? Press 1 for yes, 2 for no")
            If Console.ReadLine = 1 Then

                System.Array.Sort(array)

                For i As Integer = 0 To array.Length - 1
                    Console.WriteLine(array(i))
                Next


            ElseIf Console.ReadLine = 2 Then
                End
            End If

            Console.ReadLine()

        Case 2

            Dim results1 As String = File.ReadAllText("Z:\scores class 2.txt")

            array = Split(results1, "-")

            For i As Integer = 0 To array.Length - 1
                Console.WriteLine(array(i))
            Next

            Console.WriteLine("Would you like these to be sorted? Press 1 for yes, 2 for no")
            If Console.ReadLine = 1 Then

                System.Array.Sort(array)

                For i As Integer = 0 To array.Length - 1
                    Console.WriteLine(array(i))
                Next

            ElseIf Console.ReadLine = 2 Then
                End
            End If
            Console.ReadLine()

        Case 3

            Dim results1 As String = File.ReadAllText("Z:\scores class 3.txt")

            array = Split(results1, "-")

            For i As Integer = 0 To array.Length - 1
                Console.WriteLine(array(i))
            Next

            Console.WriteLine("Would you like these to be sorted? Press 1 for yes, 2 for no")
            If Console.ReadLine = 1 Then

                System.Array.Sort(array)

                For i As Integer = 0 To array.Length - 1
                    Console.WriteLine(array(i))
                Next

            ElseIf Console.ReadLine = 2 Then
                End
            End If
            Console.ReadLine()

    End Select

    If reply = 1 Then

        Console.WriteLine("What is your name?")
        name = Console.ReadLine

        Console.WriteLine("What class are you in, 1, 2 or 3?")
        classnum = Console.ReadLine

        If classnum < 1 Then
            MsgBox("Invalid Class number")
            End
        ElseIf classnum > 3 Then
            MsgBox("Invalid Class number")
            End
        End If

        Console.WriteLine("What is 9+10 ?")
        If Console.ReadLine = 19 Then
            score += 1
        End If

        Console.WriteLine("What is 5x10 ?")
        If Console.ReadLine = 50 Then
            score += 1
        End If

        Console.WriteLine("What is 122÷2 ?")
        If Console.ReadLine = 61 Then
            score += 1
        End If

        Console.WriteLine("What is 424 + 10 ?")
        If Console.ReadLine = 434 Then
            score += 1
        End If

        Console.WriteLine("What is 234 x 3 ?")
        If Console.ReadLine = 702 Then
            score += 1
        End If

        Console.WriteLine("What is 10 x 10 ?")
        If Console.ReadLine = 100 Then
            score += 1
        End If

        Console.WriteLine("What is 12 x 64 ?")
        If Console.ReadLine = 768 Then
            score += 1
        End If

        Console.WriteLine("What is the value of N in this equation? 2n+6=10?")
        If Console.ReadLine = 4 Then
            score += 1
        End If

        Console.WriteLine("What is 9 x 73 ?")
        If Console.ReadLine = 657 Then
            score += 1
        End If

        Console.WriteLine("What is 1 + 1 ?")
        If Console.ReadLine = 2 Then
            score += 1

        End If

        MsgBox("Your score was " & score & " Click ok to finish.")

        Dim output1 As String = name & " " & score & "-"

        Select Case classnum
            Case 1
                filePath = System.IO.Path.Combine(
           My.Computer.FileSystem.SpecialDirectories.MyDocuments, "scores class 1.txt")
                My.Computer.FileSystem.WriteAllText(filePath, output1, True)
            Case 2
                filePath = System.IO.Path.Combine(
            My.Computer.FileSystem.SpecialDirectories.MyDocuments, "scores class 2.txt")
                My.Computer.FileSystem.WriteAllText(filePath, output1, True)
            Case 3
                filePath = System.IO.Path.Combine(
            My.Computer.FileSystem.SpecialDirectories.MyDocuments, "scores class 3.txt")
                My.Computer.FileSystem.WriteAllText(filePath, output1, True)
        End Select

    End If

End Sub
一个多维对象arr
Dim names() As String = {"Student1","Student2"...}
Dim scores() As Integer = {95, 87...}
Public Class Student
    Property name As String
    Property score As Integer
End Class
Private Sub FlipStrings(ByRef students() As String)
    If students Is Nothing Then Exit Sub
    For i As Integer = 0 To UBound(students)
        Dim parts() As String = Split(students(i))
        If parts Is Nothing OrElse UBound(parts) <> 1 Then Continue For
        If IsNumeric(parts(0)) Then
            students(i) = parts(1) & " " & CInt(parts(0))
        ElseIf IsNumeric(parts(1)) Then
            Do Until len(parts(1)) = 3 'Assuming max score of 100'
                parts(1) = "0" & parts(1)
            Loop
            students(i) = parts(1) & " " & parts(0)
        End If
    Next
End Sub
FlipStrings(array)
System.Array.Sort(array)
FlipStrings(array)
Dim myVals As String() = {"5", "07", "178", "9", "899", "42", "3"}
' add two of the "numbers"
Console.WriteLine("{0} + {1} = {2}", myVals(0), 
                         myVals(1), (myVals(0) + myVals(1)))
Array.Sort(myVals)
Array.Reverse(myVals)

For Each s As String In myVals
    Console.Write("{0}  ", s)
Next
Public Class Student
    Public Property Name As String
    Public Property Score As Integer
    
    Public Sub New()
    End Sub

    ' overload
    Public Sub New(n As String, s As Integer)
        Name = n
        Score = s
    End Sub

    Public Overrides Function ToString() As String
        Return String.Format("{0} ({1})", Name, Score)
    End Function
End Class
Private Students As List(of Student)       ' declaration
Students = New List(Of Student)         ' create List instance

Dim stud As New Student                 ' create new student
stud.Name = "Harvey"                    ' set props
stud.Score = 72                         ' 72 is a NUMBER
Students.Add(stud)                      ' add to collection

' fast way, using the overload, no temp student object needed
Students.Add(New Student("Bob", 67))
Students.Add(New Student("Hoover", 82))
Students.Add(New Student("Ziggy", 97))
...
Students.Add(New Student("Zoey", 89))
Students = Students.OrderByDescending(Function(x) x.Score).ToList()
Students = Students.OrderByDescending(Function(x) x.Score).
            ThenBy(Function(q) q.Name).ToList()
' print the contents:
For Each st As Student In Students
    Console.WriteLine(st)
Next