vb的编程代码怎么写

时间:2025-01-26 02:13:32 网络游戏

求100以内的素数

```vb

Private Sub Form_Click()

Dim i As Integer

For i = 2 To 100

Dim j As Integer

For j = 2 To i - 1

If i Mod j = 0 Then

Exit For

End If

Next j

If j = i Then

Print i

End If

Next i

End Sub

```

从键盘输入任意长度的字符串,将字符顺序倒置

```vb

Private Sub Command1_Click()

Dim a$

a = InputBox("输入字符串")

Dim n%

n = Len(a)

Dim c$

c = ""

For i = n To 1 Step -1

c = c & Mid(a, i, 1)

Next i

Print c

End Sub

```

计算0~200之间所有能被11整除的数之和

```vb

Private Sub Form_Click()

Dim sum As Integer

For i = 0 To 200

If i Mod 11 = 0 Then

sum = sum + i

End If

Next i

Print sum

End Sub

```

输入一年份,判断它是否为闰年,并显示有关信息

```vb

Private Sub Command1_Click()

Dim year As Integer

year = InputBox("输入年份")

If (year Mod 4 = 0 And year Mod 100 <> 0) Or (year Mod 400 = 0) Then

MsgBox year & "年是闰年"

Else

MsgBox year & "年是平年"

End If

End Sub

```

已知x,y,z个变量中存放了3个不同的数,比较它们的大小并进行调整,使得x

```vb

Private Sub Command1_Click()

Dim x, y, z As Integer

InputBox "输入第一个数"

x = Val(InputBox("输入第二个数"))

InputBox "输入第三个数"

z = Val(InputBox("输入第三个数"))

If x >= y Or y >= z Then

MsgBox "输入的数需要不同且按升序排列"

Else

If x > y Then

y = x

End If

If y > z Then

z = y

End If

MsgBox "排序后的数为: " & x & ", " & y & ", " & z

End If

End Sub

```

这些示例代码涵盖了VB编程的一些基本概念,包括变量声明、条件语句、循环语句、函数等。通过学习和练习这些示例,你可以逐步掌握VB编程的技巧。