2

我正在尝试在 Visual Basic 中打印钻石。我的代码的上半部分运行良好,但由于某种原因,我无法让下半部分以类似的方式工作。在下半部分,在o(用于制作菱形)之后打印出空格,并且o的数量减少使程序陷入无限循环?任何人都可以帮忙吗?

下半部分可能完全错误,我还在努力

这是代码:

Option Explicit On
Imports System

Module diamond
    Sub Main()
        ' The o variable will make the "o" part of the diamond, the space variable will be for places without an "o". 
        Dim row, spaces, o as Integer  
        'Dim index as integer

        'For top half
        ' Count the row number.
        row = 1
        spaces = 9
        o = 1
        Do until row = 20
            ' Count of row increases every loop until half of diamond is done.
            row +=1
            ' Write 1 less space every line.
            For row = 1 to spaces
                console.write(" ")
            Next row
            spaces -=1
            'Write 2 more "o"'s every loop.
            For row = 1 to o 
                Console.write("o")
            Next row    
            o +=2
            Console.Writeline
        Loop
        'Top of the diamond is done

        'This is the bottom half, which will not work.
        row = 20

        Do until row = 40
            row +=1
            For row = 20 to spaces
                Console.write(" ")
            Next row
            spaces +=1
            'Write 2 less "o"'s every loop.
            For row = 20 to o 
                Console.write("o")
            Next row    
            o -=2
            Console.Writeline
        Loop
    End Sub
End Module 
4

1 回答 1

3

问题是您将row其用作循环的行计数器以及while循环的循环索引for。上半场完全奏效是偶然的。下半场row永远不会达到 40,因为它不断被for循环劫持。

尝试For i = 20 to spaces,等等,而不是。

于 2012-10-11T18:49:04.473 回答