【问题标题】:Word search puzzle on vbvb上的单词搜索难题
【发布时间】:2021-12-16 18:46:13
【问题描述】:

我正在尝试创建一个程序,要求用户输入 10 个他们希望隐藏在单词搜索谜题中的单词。如何在网格中放置单词?

这就是我所做的,但我一直收到此错误

messageSystem.IndexOutOfRangeException
Message=Index 超出了数组的范围。

代码:

Sub Words()
        wordcount = 0
        Do
            Console.WriteLine("choose word")
            word = Console.ReadLine
            Showdirection()
            Console.WriteLine("choose row")
            row = Console.ReadLine
            Console.WriteLine("choose column")
            col = Console.ReadLine
            grid(row, col) = word(0)
            For i = 1 To Len(word)
                Select Case choice
                    Case "1"
                        col = col + 1
                    Case "2"
                        col = col - 1
                    Case "3"
                        row = row - 1

                    Case "4"
                        row = row + 1

                    Case "5"
                        col = col + 1
                        row = row - 1
                    Case "6"
                        col = col - 1
                        row = row + 1

                    Case "7"
                        col = col + 1
                        row = row + 1

                    Case "8"
                        col = col - 1
                        row = row + 1
                End Select
                grid(row, col) = word(i)
            Next
            wordcount = wordcount + 1
        Loop Until wordcount = 10
    End Sub

另外,一旦所有的单词都放在网格中,我如何用随机字母填充剩余的空间?

这是我目前的代码


  Dim grid(14, 14) As String
    Dim choice As Integer
    Dim word As String
    Dim wordcount As Integer = 0
    Dim row As Integer
    Dim col As Integer
    Sub outputgrid()
        For i = 0 To 14
            For j = 0 To 14
                Console.Write(grid(i, j))
            Next j
            Console.WriteLine()
        Next i
    End Sub
    Sub setupgrid()
        For i = 0 To 14
            For j = 0 To 14
                grid(i, j) = ""
            Next j
        Next i
    End Sub
    Sub Main(args As String())
        setupgrid()
        outputgrid()
        Words()
        Console.ReadLine()
    End Sub
    Sub Words()
        wordcount = 0
        Do
            Console.WriteLine("choose word")
            word = Console.ReadLine
            Showdirection()
            Console.WriteLine("choose row")
            row = Console.ReadLine
            Console.WriteLine("choose column")
            col = Console.ReadLine
            grid(row, col) = word(0)
            For i = 1 To Len(word)
                Select Case choice
                    Case "1"
                        col = col + 1
                    Case "2"
                        col = col - 1
                    Case "3"
                        row = row - 1

                    Case "4"
                        row = row + 1

                    Case "5"
                        col = col + 1
                        row = row - 1
                    Case "6"
                        col = col - 1
                        row = row + 1

                    Case "7"
                        col = col + 1
                        row = row + 1

                    Case "8"
                        col = col - 1
                        row = row + 1
                End Select
                grid(row, col) = word(i)
            Next
            wordcount = wordcount + 1
        Loop Until wordcount = 10
    End Sub
    Sub Showdirection()
        Console.WriteLine("choose direction")
        Console.WriteLine("1 horizontal- left to right")
        Console.WriteLine("2 Horizontal -Right to Left")
        Console.WriteLine("3 Vertical -Down")
        Console.WriteLine("4 Vertical -Up")
        Console.WriteLine("5 Diagonal – Down L To R")
        Console.WriteLine("6 Diagonal – Down R To L ")
        Console.WriteLine("7 Diagonal – Up L To R ")
        Console.WriteLine("8 Diagonal – Up R To L ")
        choice = Console.ReadLine()
    End Sub

【问题讨论】:

  • 你可能想改写你的问题。您能否准确地更新您的问题,说明上述代码如何不符合您的要求。
  • Words() 中,您要求输入单词、方向和起始位置,但我认为没有逻辑可以确定该位置/方向上的单词是否有足够的 ROOM。错误很可能源于您试图在拼图边界之外插入下一个字母。此外,还有零逻辑来确定该单词是否会覆盖先前写入的单词。因此,您需要检查该单词是否有足够的空间以及该位置是否没有现有字母。单词可以重叠,但它们必须在交叉点有相同的字母,这更难检查。

标签: vb.net console-application


【解决方案1】:

首先,您应该在代码顶部添加Option Strict On。您正在从字符串隐式转换为整数并返回。一旦你这样做了,你会发现有些事情需要一些显式的转换(而且,我不会这样做,但你应该验证用户的输入)

choice = CInt(Console.ReadLine())
' there are more examples of this you need to fix
' but what if the user enters a non-number? You should handle that case

case 语句在 Integer 上运行,但您的 case 是字符串。更改为整数

Select Case choice
    Case 1
        col = col + 1
    Case 2
        col = col - 1
...

至于你的代码不起作用,我会帮你想出一个解释(这是你应该在你的问题中提出的):

System.IndexOutOfRangeException
Message=Index 超出了数组的范围。

很容易看出您的 For 循环设置不正确。请记住,数组在 vb.net 中是从零开始的。

改变

For i = 1 To Len(word)

For i = 0 To Len(word) - 1

要填充其余的空单元格,请使用方法

Private Sub fillEmptyCells()
    Dim random As New Random()
    For i = 0 To grid.GetUpperBound(0)
        For j = 0 To grid.GetUpperBound(1)
            If grid(i, j) = "" Then
                ' ascii characters 97 to 122 are all the lower-case letters
                grid(i, j) = Chr(random.Next(97, 123)).ToString()
            End If
            ' this will clean up your entry in the case the user entered a capital letter
            grid(i, j) = grid(i, j).ToLower()
        Next
    Next
End Sub

最后调用它

Sub Main(args As String())
    setupgrid()
    outputgrid()
    Words()
    fillEmptyCells()
    Console.ReadLine()
End Sub

【讨论】:

  • 如何修复错误 System.IndexOutOfRangeException Message=Index 超出了数组的范围?另外,如果我想要大写字母,我应该将这个 `grid(i, j) = Chr(random.Next(97, 123)).ToString()` 更改为 `grid(i, j) = Chr(random.Next(65 , 90)).ToString()`
  • 修复在我的回答中,即For i = 0 To Len(word) - 1。 Random.Next 第二个参数是独占的,所以你需要它是 91 即 Chr(random.Next(65, 91)).ToString() 然后 grid(i, j) = grid(i, j).ToLower() 应该更改为 grid(i, j) = grid(i, j).ToUpper()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
相关资源
最近更新 更多