VB.net控制台 - 显示最大数字(变量)

时间:2012-10-02 14:21:53

标签: vb.net console-application

我正在努力想出这个问题的代码:

要求用户输入10个号码然后取消最大号码

到目前为止,我已经提出了这个问题,但看看它会如何表现,我只能看到错误:

Sub Main()
    Dim One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten As String
    Console.WriteLine("Please enter your first number")
    One = Console.ReadLine
    Console.WriteLine("Please enter your second number")
    Two = Console.ReadLine
    Console.WriteLine("Please enter your third number")
    Three = Console.ReadLine
    Console.WriteLine("Please enter your fourth number")
    Four = Console.ReadLine
    Console.WriteLine("Please enter your fifth number")
    Five = Console.ReadLine
    Console.WriteLine("Please enter your sixth number")
    Six = Console.ReadLine
    Console.WriteLine("Please enter your seventh number")
    Seven = Console.ReadLine
    Console.WriteLine("Please enter your eighth number")
    Eight = Console.ReadLine
    Console.WriteLine("Please enter your nineth number")
    Nine = Console.ReadLine
    Console.WriteLine("Please enter your tenth number")
    Ten = Console.ReadLine

    If Ten > Nine Then
        Console.WriteLine("Your biggest number is" & Ten)
    Else
        If Nine > Eight Then
            Console.WriteLine("Your biggest number is" & Nine)
        Else

我需要看看最大的数字是什么。另一名学生正在使用阵列来实现这一目标,但我似乎无法解决这个问题。

因此,在用户输入数字后,我该如何实现?

我尝试(上面)使用If语句,但我看到代码是广泛的,如果一个人在开头输入一个更大的数字,然后在最后输入一个,则会出现错误,因为它只会显示第一个更大的数字。 ..

3 个答案:

答案 0 :(得分:3)

如果您不想使用数组或集合,我建议您在每次输入后仅保留最高值。例如,这里有一些伪代码:

WriteLine("Please enter your first number")
Current = ReadLine
If Current > Highest Then
    Highest = Current
End If
... Do it again

答案 1 :(得分:1)

主要有两种方法:

  • 将所有数字放入数组中,并在输入所有数字后循环显示它们。

  • 记录输入过程中的最大数字,以便将每个新数字与之前的最高数字进行比较,如果更高则将其替换。

答案 2 :(得分:1)

您不需要数组,

Console.Writeline("Number 1?")
Dim Number as integer = Console.Readline()
For Key as Integer = 2 to 10
    Console.Writeline("Number " & Key & "?")
    'Get current number and compare with larger number?
Next
Console.Writeline("Largest number: " & Number)

阅读本文,然后尝试自己编写,你会学到更多东西:)