Visual Studio:如何在for循环中的文本之前放置空格

时间:2019-03-01 04:49:04

标签: visual-studio visual-studio-2015

在这个问题上,我需要您的帮助。我想做的是在给定的坐标或空间内创建一个矩形。到目前为止,这是我的代码,我在这里错过了什么?请参见下面的示例输入和输出。

Dim d As String = ""
Dim s As String = ""
Dim counter As Integer = 0

For i = 1 To y
    s = s & vbNewLine
Next

For row = 1 To height
    For col = 1 To width
         If x <> 0 Then



         Else
             d = d & "X "
         End If
        Next
        d = d & vbNewLine
    Next
outputTBX.Text = s & d

这是我的示例输入,但是,如果您查看示例输出,则X之前应该有2个空格。预先谢谢你!

宽度:4

高度:4

X轴:2

Y轴:2

enter image description here

1 个答案:

答案 0 :(得分:1)

请参阅下文。我认为您缺少添加空格的col偏移循环。

Sub Main
    DrawRectangle(4,4,2,2)
End Sub

Sub DrawRectangle(ByVal height As Integer, ByVal width As Integer, ByVal x As Integer, ByVal y As Integer) 

Dim d As String = ""
Dim s As String = ""
Dim counter As Integer = 0

' row offset
For i = 1 To y
    s = s & Environment.NewLine 
Next

' row loop
For row = 1 To height
    'col offset
    For i = 1 To x
        d = d & " "
    Next
    'col loop
    For col = 1 To width
         d = d & "X"
    Next
    d = d & Environment.NewLine 
Next

' uncomment outputTBX and comment console for your work
'outputTBX.Text = s & d
Console.WriteLine(s & d)

End Sub