【问题标题】:How can I get ten numbers and displays the biggest and lowest one?如何获得十个数字并显示最大和最小的数字?
【发布时间】:2020-04-18 16:19:26
【问题描述】:

对不起,我是一个新手,我正在尝试编写一个程序,以使用输入框或文本框从用户那里获取十个整数,并使用 Visual Basic 在标签中显示最大和最小的整数。如果您能帮我解决这个问题,我将不胜感激。 谢谢你。这是我的解决方案。我不知道如何比较这十个数字。

Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
        Dim i, Container, Max, Numbers
        Max = 0
        i = 1


        While (i <= 10)
            Numbers = InputBox("please enter a number", "Enter a number")
            Max = Numbers
            Container = Container & " " & Numbers
            i = i + 1
        End While
        lblresult.Text = Container
    End Sub

【问题讨论】:

    标签: vb.net visual-studio-2017 date-comparison


    【解决方案1】:

    从概念上讲,您应该使用 List(Of Integer) 或 List(Of Double),执行循环 10 次,将值添加到列表中。

    假设这是我们的列表

    Dim container As New List(Of Integer)
    

    获取输入

    Dim userInput = ""
    Dim input As Integer
    
    userInput = InputBox("please enter a number", "Enter a number")
    If Integer.TryParse(userInput, input) Then
        container.Add(input)
    End If
    

    循环之后

    Console.WriteLine($"Min: {container.Min()} Max: {container.Max()}")
    

    这对你有意义吗?

    编辑,基于询问 Windows 窗体示例。

    您可以执行以下操作而不是 InputBox,需要一个标签、一个按钮和一个 TextBox。

    Public Class MainForm
        Private container As New List(Of Integer)
    
        Private Sub CurrentInputTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) _
            Handles CurrentInputTextBox.KeyPress
    
            If Asc(e.KeyChar) <> 8 Then
                If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                    e.Handled = True
                End If
            End If
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            CurrentLabel.Text = "Enter number 1"
        End Sub
        Private Sub ContinueButton_Click(sender As Object, e As EventArgs) _
            Handles ContinueButton.Click
    
            If Not String.IsNullOrWhiteSpace(CurrentInputTextBox.Text) Then
    
                container.Add(CInt(CurrentInputTextBox.Text))
                CurrentLabel.Text = $"Enter number {container.Count + 1}"
    
                If container.Count = 10 Then
                    ContinueButton.Enabled = False
                    CurrentLabel.Text =
                        $"Count: {container.Count} " &
                        $"Max: {container.Max()} " &
                        $"Min: {container.Min()}"
                Else
                    ActiveControl = CurrentInputTextBox
                    CurrentInputTextBox.Text = ""
                End If
    
            End If
        End Sub
    End Class
    

    【讨论】:

    • 个人选择,个人不关心InputBox。我自己永远不会使用,也永远不会使用,因为无法控制用户输入,而恕我直言,更好的选择是使用 TextBox 使用事件来防止非数字(并且可以选择将属性 ShortcutEnabled 设置为 false 以防止从窗口粘贴剪贴板),甚至更好的是自定义文本框,它只接受数字,但对于这个练习来说太过分了。 @Immortality 可以根据需要使用 InputBox,没有什么能阻止它。
    • 我同意InputBox,但这似乎是一个初学者问题,滚动你自己的输入框可能有点高级。我不喜欢 .KeyPress。我认为这对用户来说非常烦人。啊!我的键盘不工作了! ErrProvider 的验证事件怎么样?
    • 确定 ErrorProvider 和验证事件是一个有效的选项。
    【解决方案2】:

    我真的不想为你做作业,但我担心你可能会糊涂。

    首先让我们回顾一下您的代码。见 cmets

    Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
        Dim i, Container, Max, Numbers 'Don't declare variables without an As clause
        Max = 0 'Max is an object
        i = 1 'i is and object
        While i <= 10 'the parenthesis are unnecessary. You can't use <= 2 with an object
            Numbers = InputBox("please enter a number", "Enter a number")
            Max = Numbers
            Container = Container & " " & Numbers 'Container is an object; you can't use & with an object
            i = i + 1 'Again with the object i can't use +
        End While
        lblresult.Text = Container
    End Sub
    

    现在我的方法

    我在Form 级别创建了一个List(Of T),以便可以从不同的过程中看到它。 T 代表类型。我可以是内置类型或您通过创建Class 创建的类型。

    第一次点击事件用输入的数字填充列表。我使用.TryParse 来测试输入是否是正确的值。第一个参数是一个字符串;来自用户的输入。第二个参数是一个变量,用于保存转换后的字符串。 .TryParse 非常聪明。它根据输入字符串是否可以转换为正确的类型返回TrueFalse,并用转换后的值填充第二个参数。

    第二次点击事件循环通过列表构建一个字符串以显示在Label1 中。然后我们使用List(Of T) 可用的方法来获取您想要的数字。

    Private NumbersList As New List(Of Integer)
    
    Private Sub FillNumberList_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer
        While i < 10
            Dim input = InputBox("Please enter a whole number")
            Dim inputInt As Integer
            If Integer.TryParse(input, inputInt) Then
                NumbersList.Add(inputInt)
                i += 1 'We only increment i if the parse is succesful
            End If
        End While
        MessageBox.Show("Finished Input")
    End Sub
    
    Private Sub DisplayResults_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Label1.Text = "You input these numbers "
        For Each num In NumbersList
            Label1.Text &= $"{num}, "
        Next
        Label2.Text = $"The largest number is {NumbersList.Max}"
        Label3.Text = $"The smallest number is {NumbersList.Min}"
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-14
      • 2019-03-20
      • 2018-04-29
      • 2015-05-25
      • 2014-01-23
      • 2018-09-06
      • 1970-01-01
      • 2021-04-30
      相关资源
      最近更新 更多