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

如题所述

外循环确定将要打印多少行,内循环则要确定每一行要打印多少个*和多少个空格,自己把行数和每一行要打印的*和空格的个数列出来找规律,相当于高中时的数学归纳法。比如说:要打印三行*组成一个三角形,代码如下(图形如下):
*
***
*****
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:如何在文本框中用星号打印等腰三角形???
Dim i As Integer Dim j As Integer For i = 1 To 10 Text1.Text = Text1.Text & Space(12 - i) '关键在于这一句 For j = 1 To i Text1.Text = Text1.Text & "*"Next j Text1.Text = Text1.Text & Chr(13) + Chr(10) 'vbCrLf Next i ...

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用*号 打印输出图形
用什么控键输出?textbox, label还是messagebox? 下面的程序用msgbox输出,另外三角形怎么才四行数据,是不是应该是这样:Private Sub CommandButton1_Click()Dim intRowNumber As Integer Dim i As Integer Dim j As Integer Dim strOutput As String intRowNumber = 5 '画菱形部分 For i = 1 To...

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循环结构输出下列三角形
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

相似回答
大家正在搜