【问题标题】:how can i display the number of days in a month that i input with two arrays?如何显示我用两个数组输入的一个月中的天数?
【发布时间】:2015-04-20 21:25:57
【问题描述】:

这就是我所坚持的,我需要使用两个数组,一个用于几个月,一个用于一个月中的几天。我想链接它们,所以当用户输入一月时,文本框会填充数字 31。我已经制作了两个数组,但我无法链接它们。我的教授说我必须使用一个 for each...next 使用线性搜索的语句..但我无法为我的生活弄明白。我试过制作 MonthName(0) = Monthdays(0) 但它什么也没做。如果用户拼写错误,我还需要使用 String.Compare,以便告诉他们单词不匹配。我迷路了。到目前为止,这是我的代码。任何指导表示赞赏。

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MonthName(11)
        Dim Monthdays(11)
        Dim searchData(11) As Integer

        MonthName(0) = "January"
        MonthName(1) = "February"
        MonthName(2) = "March"
        MonthName(3) = "April"
        MonthName(4) = "May"
        MonthName(5) = "June"
        MonthName(6) = "July"
        MonthName(7) = "August"
        MonthName(8) = "September"
        MonthName(9) = "October"
        MonthName(10) = "November"
        MonthName(11) = "December"

        Monthdays(0) = 31
        Monthdays(1) = 28
        Monthdays(2) = 31
        Monthdays(3) = 30
        Monthdays(4) = 31
        Monthdays(5) = 30
        Monthdays(6) = 31
        Monthdays(7) = 31
        Monthdays(8) = 30
        Monthdays(9) = 31
        Monthdays(10) = 30
        Monthdays(11) = 31

        For i = 0 To searchData.GetUpperBound(0)
            TextBox2.Text = Convert.ToString(Monthdays)
        Next

        If TextBox1.Text = String.Empty Then
            MessageBox.Show("Invald Entry.", "Error", 
                  MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

    End Sub
End Class

【问题讨论】:

  • 使用For n 循环。当MonthName(n) 匹配您要查找的内容时,n 也将成为MonthDays 的索引。那就是MonthName(n) 里面有MonthDays(n) 天。您也可以使用Array.IndexOf() 跳过循环。

标签: arrays vb.net for-loop


【解决方案1】:
For n as Integer = 0 to MonthName.Length - 1
    if MonthName(n) = TextBox1.Text then
         TextBox1.Text = Monthdays(n).ToString()
         Exit For
    end if
next

这将循环所有 MonthNames。如果名称与文本框中的文本匹配,则其 textx 将替换为相应的 Monthdays。

如果找到对应的月份,Exit For 语句将停止循环以避免不必要的性能

您可能需要将 TextBox1 重命名为您的 TextBox 对象的名称。

您应该使用数据类型声明您的数组:Dim MonthName(11) as StringDim Monthdays(11) as Integer

我希望这会有所帮助。我没有测试这段代码。对不起我的英语不好。

【讨论】:

  • MonthName中最后一个索引是MonthName.Length - 1,所以第一条语句应该是For n As Integer = 0 To MonthName.Length - 1
  • 谢谢你的救命恩人!!我现在理解得更好了
【解决方案2】:

您的教授对For Each... Next 的看法是建议吗?还是要求?因为 IMO 会让事情变得更复杂。

无论如何,正如 Plutonix 在 cmets 中所说,您可以使用简单的 For n 循环或 Array.IndexOf()(尽管我相信重点是使用循环)。

For n:

TextBox2.Text = "" 'Sanity check
For i as Integer = 0 to 11
    If MonthName(i) = TextBox1.Text Then 'If the current Month name equals user input...
        TextBox2.Text = Monthdays(i) '...we get the corresponding days and put them on the TextBox
        Exit For 'We found what we were looking for... no need to continue searching
    End If
Next

If TextBox2.Text = "" Then 'Notify if we didn't find a match
    MessageBox.Show("Invald Entry.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

使用Array.IndexOf()

Dim index as Integer 'define a variable to store the index
index = Array.IndexOf(MonthName, TextBox1.Text) 'We try to find the position of MonthName that equals user input
If index >= 0 Then 'If there was a match, we get the month days of that position
    TextBox2.Text = Monthdays(index)
Else 'if not, notify...
    TextBox2.Text = ""
    MessageBox.Show("Invald Entry.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

据我所知,使用For Each... Next 需要使用Array.IndexOf(),从而使循环变得不必要。

希望其中一些选项有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-27
    • 2011-09-08
    • 2021-11-06
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 2020-01-12
    • 2022-06-25
    相关资源
    最近更新 更多