问题
It would be great if someone could help me out with my problem.
I need to create a VB console application that sorts 3 integer values into ascending order using a sub-procedure. The sub-procedure (e.g. called sortnumber()) needs to accept 3 integer parameters and then sort the values in ascending order.
How would I go about doing this, could someone point me in the right direction to a solution with code as simple as possible.
Thanks.
Module Module1
Sub Main()
Dim num1, num2, num3 As Integer
Console.WriteLine("Enter first number:")
num1 = Console.ReadLine
Console.WriteLine("Enter second number:")
num2 = Console.ReadLine
Console.WriteLine("Enter third number:")
num3 = Console.ReadLine
sortnumber()
End Sub
Sub sortnumber(ByVal num1 As Integer, ByVal num2 As Integer, ByVal num3 As Integer)
Dim
End Sub
I have no idea if this is right or where I am going with this..
回答1:
Module Module1
Public Sub sortnumber(ByVal n1 As Integer, ByVal n2 As Integer, ByVal n3 As Integer)
Dim l As List(Of Integer) = New List(Of Integer)
l.Add(n1)
l.Add(n2)
l.Add(n3)
l.Sort()
For Each h In l
Console.WriteLine(h.ToString)
Next
End Sub
Sub Main()
Dim num1, num2, num3 As Integer
Console.WriteLine("Enter first number:")
num1 = Console.ReadLine
Console.WriteLine("Enter second number:")
num2 = Console.ReadLine
Console.WriteLine("Enter third number:")
num3 = Console.ReadLine
Console.WriteLine("Calculating ......")
sortnumber(num1, num2, num3)
MsgBox("done")
End Sub
End Module
回答2:
When you are programming in VB.NET, it is possible to try to put the wrong sort of value into a variable. There are two places to guard against this: there could be such an error in your programming, and the user could enter the wrong information (e.g. entering "A" instead of a number).
VB in Visual Studio offers a way to detect the first of these problems: Option Strict On.
In your program, you are trying to read a String (from ConsoleReadLine) into an Integer (e.g. num1). This is not really correct. Without Option Strict On, VB will try to make sense of what you have done, but sometimes it will guess the wrong thing about what you intended. If you use Option Strict On, it will notify you that something is awry. Fortunately, there are functions to convert from a String into an Integer (lots of other conversion are available).
For inputting a load of numbers, it might be nice if it was easy to adjust your program. Say you suddenly decided that you wanted to sort four numbers; you would then have to add another variable like num4 and re-write your sorting procedure. An Array is a useful type to use when you know in advance how many entries it will have because you can use one name to refer to it and a number to say which of the entries you want to use. A List is a sort of super-array as there are a lot of built-in operations which are useful.
Without addressing the problem of the user entering an invalid value, your program could look something like this:
Option Strict On
Module Module1
Sub Main()
Dim nums As New List(Of Integer)
Console.Write("Enter first number: ")
nums.Add(Integer.Parse(Console.ReadLine()))
Console.Write("Enter second number: ")
nums.Add(Integer.Parse(Console.ReadLine()))
Console.Write("Enter third number: ")
nums.Add(Integer.Parse(Console.ReadLine()))
nums.Sort()
Console.WriteLine("The sorted numbers are:")
' The index starts at 0, not 1.
For i As Integer = 0 To nums.Count - 1
Console.WriteLine(nums(i).ToString())
Next
Console.ReadLine()
End Sub
End Module
来源:https://stackoverflow.com/questions/20018838/how-would-i-create-a-sub-procedure-to-sort-numbers-in-ascending-order-vb-cons