vb 循环结构的试题看不懂,请高手指点
DimxAsIntegerx=1n=0DoWhilex<28x=x*3n=n+1LoopPrintn,x答案是4和81。为什么n到3就停止循环了?最后n=4是怎么的出来的...
Dim x As Integer
x = 1
n = 0
Do While x < 28
x = x * 3
n = n + 1
Loop
Print n, x
答案是 4和81。为什么n到3就停止循环了?最后n=4是怎么的出来的? 展开
x = 1
n = 0
Do While x < 28
x = x * 3
n = n + 1
Loop
Print n, x
答案是 4和81。为什么n到3就停止循环了?最后n=4是怎么的出来的? 展开
展开全部
Dim x As Integer
x = 1
n = 0
Do While x < 28 条件成立时下面代码运行,条件不成立时跳出循环,
x = x * 3 '(x=1*3/ x=3*3 / x=9*3 / x=27*3=81 循环至3次后x=27条件仍成立,第四次检查 时X为第3次循环结束时的结果x=27<28,条件成立继续执行一次x=27*3,
在第五次的条件判断中x=27*3>28,跳出循环,此时X=81)
n = n + 1 '(n=0+1 / n=1+1 / n= 2+1 / n= 3+1 同为4次运算,故循环结束时n=4)
Loop
Print n, x
x = 1
n = 0
Do While x < 28 条件成立时下面代码运行,条件不成立时跳出循环,
x = x * 3 '(x=1*3/ x=3*3 / x=9*3 / x=27*3=81 循环至3次后x=27条件仍成立,第四次检查 时X为第3次循环结束时的结果x=27<28,条件成立继续执行一次x=27*3,
在第五次的条件判断中x=27*3>28,跳出循环,此时X=81)
n = n + 1 '(n=0+1 / n=1+1 / n= 2+1 / n= 3+1 同为4次运算,故循环结束时n=4)
Loop
Print n, x
展开全部
完成了,在窗体上添加4个命令按钮,分别对应你的四个题,在窗体上直接输出结果,测试没问题,有疑问消息我
Private Sub Command1_Click()
Dim i As Integer
Cls
For i = 0 To 500
If isprime(i) Then Print i
Next
End Sub
Function isprime(n As Integer) As Boolean '求质数
isprime = True '赋初值
For i = 2 To Int(Sqr(n))
If n Mod i = 0 Then '如果能除尽
isprime = False '证明是素数
Exit For
End If
Next i
End Function
Private Sub Command2_Click()
Dim x(1 To 10) As Integer
Randomize
Cls
For i = 1 To 10
x(i) = Int(Rnd * 100)
Print x(i)
Next
Print "最大:"; max(x())
Print "最小:"; Min(x())
End Sub
Function Min(a() As Integer) As Integer '求最小
Min = a(1)
For i = 2 To 10
If a(i) < Min Then Min = a(i)
Next
End Function
Function max(a() As Integer) As Integer '求最大
max = a(1)
For i = 2 To 10
If a(i) > max Then max = a(i)
Next
End Function
Private Sub Command3_Click()
Cls
For m = 1 To 4
For i = 1 To 5
Print i;
Next
Print
Print Space(m);
Next
Print
s = "abcde"
For i = 1 To 5
Print s
s = Right(s, 1) & Left(s, 4)
Next
Print
For i = 1 To 7 Step 2
Print Space((8 - i - 1) / 2);
Print String(i, "*")
Next
End Sub
Private Sub Command4_Click()
Cls
For x = 0 To 9
For y = 0 To 9
For z = 0 To 9
If x * 100 + y * 10 + z + y * 100 + z * 10 + z = 532 Then Print x, y, z
Next
Next
Next
End Sub
Private Sub Command1_Click()
Dim i As Integer
Cls
For i = 0 To 500
If isprime(i) Then Print i
Next
End Sub
Function isprime(n As Integer) As Boolean '求质数
isprime = True '赋初值
For i = 2 To Int(Sqr(n))
If n Mod i = 0 Then '如果能除尽
isprime = False '证明是素数
Exit For
End If
Next i
End Function
Private Sub Command2_Click()
Dim x(1 To 10) As Integer
Randomize
Cls
For i = 1 To 10
x(i) = Int(Rnd * 100)
Print x(i)
Next
Print "最大:"; max(x())
Print "最小:"; Min(x())
End Sub
Function Min(a() As Integer) As Integer '求最小
Min = a(1)
For i = 2 To 10
If a(i) < Min Then Min = a(i)
Next
End Function
Function max(a() As Integer) As Integer '求最大
max = a(1)
For i = 2 To 10
If a(i) > max Then max = a(i)
Next
End Function
Private Sub Command3_Click()
Cls
For m = 1 To 4
For i = 1 To 5
Print i;
Next
Print Space(m);
Next
s = "abcde"
For i = 1 To 5
Print s
s = Right(s, 1) & Left(s, 4)
Next
For i = 1 To 7 Step 2
Print Space((8 - i - 1) / 2);
Print String(i, "*")
Next
End Sub
Private Sub Command4_Click()
Cls
For x = 0 To 9
For y = 0 To 9
For z = 0 To 9
If x * 100 + y * 10 + z + y * 100 + z * 10 + z = 532 Then Print x, y, z
Next
Next
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一次循环 X =1 ,n=0 循环后 X=3,n=1
第二次循环后x=9,n=2
第三次循环后x=27,n=3 此时X<28仍可以继续循环
第四次循环后x=81,n=4此时x>28不能再循环,结束循环。
只要X<28,就可以执行循环。
第二次循环后x=9,n=2
第三次循环后x=27,n=3 此时X<28仍可以继续循环
第四次循环后x=81,n=4此时x>28不能再循环,结束循环。
只要X<28,就可以执行循环。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询