运用循环作图(prj5-4)
1、程序中用Scale方法用来改变坐标系。Scale (-1500,3000)-(1500,-100)把原来左上角(0,0)改为,右下角为(-1500,-100),实现如图的坐标系。
2、在y=ax^2,a被定为1/900,用以提高作图的效果。
Private Sub Form_Load()
Dim i As Integer, x As Long, y As Long
Form1.Show
Scale (-1500, 3000)-(1500, -100)
'改变坐标系
Line (0, -100)-(0, 3000)
'画Y轴
Line (-1500, 0)-(1500, 0)
'画X轴
x = -1500
CurrentX = x: CurrentY = x * x / 900 '用循环画抛物线
Do While x <= 1500
y = x * x / 900
Line -(x, y)
x = x + 30
Loop
x = -1500
CurrentX = x: CurrentY = x
Do While x <= 1500
'用循环画直线
y = x
Line -(x, y)
x = x + 30
Loop
End Sub
运用循环对字符串进行操作

Len是一个系统函数,它返回字符串其中字符的个数。程序代码中最关键的语句是循环体内的s2=Mid(s1,i,1) &
s2,s2的初值为空。如果s1 = "abcdefg",当i=1时Mid函数值为 "a",s2的值也为"a",当i=2时Mid值为 "b",s2的值也为"ba";以此类推当I>Len(s1)时退出循环,Label1.Caption = s2,实现了字符的倒背。
Private Sub Command1_Click()
Dim s1 As String, s2 As String
Dim i As Integer, l As String
s1 = Text1.Text
s2 = ""
i = 1
Do While i <= Len(s1)
s2 = Mid(s1, i, 1) & s2
i = i + 1
Loop
Label1.Caption = s2
End Sub
Private Sub Command2_Click()
End
End Sub
|