gpt4 book ai didi

vb.net - Visual Basic 循环永无止境

转载 作者:行者123 更新时间:2023-12-04 05:29:08 24 4
gpt4 key购买 nike

我正在尝试在 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

最佳答案

问题是您正在使用 row作为 while 的行计数器循环,也作为 for 的循环索引循环。前半部分工作完全是偶然的。下半年row永远不会达到 40,因为它不断被 for 劫持循环。

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

关于vb.net - Visual Basic 循环永无止境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12846408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com