求解VB编程中,利用双重循环用*号输出三角形,请说明为什么(很急!)

如题所述

第1个回答  推荐于2017-05-17
外循环确定将要打印多少行,内循环则要确定每一行要打印多少个*和多少个空格,自己把行数和每一行要打印的*和空格的个数列出来找规律,相当于高中时的数学归纳法。比如说:要打印三行*组成一个三角形,代码如下(图形如下):

*
***
*****

Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
For i = 1 To 3
For j = 1 To 3 - i + 1
Print " ";
Next j
For k = 1 To 2 * i - 1
Print "*";
Next k
Print
Next i

End Sub

现在只能按照这个程序打印出的图形分析,需要看图形找规律。由于第三行的左边也有一个空格,所以从上到下每行的第一颗*的左边的空格数依次是3,2,1,相当于数组中的a1,a2,a3(即打印空格那个循环的变量);而行数是第1,2,3行,相当于数组的下标(外循环的变量) 则有
1 3
2 2
3 1
. .
. .
. .
i j

不难看出有:j=3-i+1 (等差数列)

打印*的问题也可以这样推断出来。本回答被提问者采纳

vb怎样打出“*”号倒三角形来?
可以参考下面的代码:Private Sub Command1_Click()Dim i As Long, j As Long Dim strTemp As String, strXing As String For i = 0 To 3 strXing = ""For j = 1 To (7 - Abs(i) * 2)strXing = strXing & "*"Next strTemp = Space(Abs(i)) & strXing & Space(Abs(i))Pr...

vb中输出两个上下颠倒的星号三角形
Private Sub Command1_Click()For i = 1 To 8 For j = 1 To 9 - i Print " ";Next j For j = 1 To 2 * i - 1 Print "*";Next j For j = 1 To 10 Print " ";Next j For j = 1 To 2 * (9 - i) - 1 Print "*";Next j Print Next i Print End Sub ...

vb用星号打印倒直角三角形求解
Private Sub Command1_Click()For i = 0 To 8 '共打印9行 For j = 1 To i Print " "; '每行上先打印i个空格 Next j For j = 1 To 9 - i Print "*"; '再打印9-i个*号 Next j Print '一行打印结束就换行 Next i End Sub ...

vb用*号 打印输出图形
strOutput = strOutput + " " 'msgbox里一个星号的宽度是两个空格宽度,所以这里加两个空格,如果用别的方法输出看情况修改 Next j For j = 1 To i * 2 strOutput = strOutput + "*"Next j Else For j = 1 To i - 3 strOutput = strOutput + " " 'msgbox里一个星号的...

vb使用星号输出一个五行的直角三角形
For i = 1 To 5 Print String(i, "*")Next i

vb 如何用循环输出以下三角形
Private Sub Command1_Click()' 杨辉三角形 Dim i As Integer Dim j As Integer Dim n As Integer n = 5 Cls For i = 1 To n Print String(i - 1, " ");For j = 1 To (n + 1) - i Print CStr(i); " ";Next j Print Next i End Sub Private Sub Command2_...

VB里用星号输出平行四边形、正三角、倒三角的程序语句?
一、Private Sub Form_Click()show Dim m, n, i, j As Integer m = 4 :n = 7 For i = 1 To m For j = 1 To m - i + 1 Print " ";Next j For j = 1 To n Print "*";Next j Print Next i Print For i = 1 To m For j = 1 To m - i + 1 Print " ";N...

vb 循环 三角形 公式
第3行 3个“*”由此可见 这里 “*”的个数变化规律是 等差数列 所以第 n 行 “*”的个数是 -2*(n-1)+7 每行“*”左边的空格的个数也是这样推出来的 把得到的公式应用到双循环中就行了 补充回答: 同样是数学归纳法 你要问的问题就是 左边倒三角的公式是怎么来的 假如...

用VB编写一个无参数Sub过程Triangle,在窗体上输出用“*”组成的如下...
Private Sub Command1_Click()Call Triangle(6)End Sub Private Sub Triangle(N As Integer)Dim i As Integer Me.Cls For i = 1 To N Print Tab(N - i + 1); String(2 * i - 1, "*")Next End Sub

VB循环结构输出下列三角形
Private Sub Command1_Click()For i = 1 To 7 Step 2 Print Tab(10 - i); String(i, "* ")Next For i = 5 To 1 Step -2 Print Tab(10 - i); String(i, "* ")Next End Sub

相似回答