vbs代码大全
2024-05-04 20:36:53 游戏
Option Explicit'声明变量
Dim i, j As Integer
Dim str As String'打印Hello World
MsgBox "Hello World!"'输出1-100的数字
For i = 1 To 100
MsgBox i
Next i'计算1+2+3+...+100的和
Dim sum As Integer
sum = 0
For i = 1 To 100
sum = sum + i
Next i
MsgBox "1+2+3+...+100的和为:" & sum'判断一个数是否是偶数
Dim num As Integer
num = InputBox("请输入一个整数:")
If num Mod 2 = 0 Then
MsgBox num & "是偶数。"
Else
MsgBox num & "是奇数。"
End If'计算一个数的阶乘
Dim fact As Integer
fact = 1
num = InputBox("请输入一个整数:")
For i = 1 To num
fact = fact * i
Next i
MsgBox num & "的阶乘是:" & fact'逆序输出字符串
str = InputBox("请输入一个字符串:")
Dim revStr As String
revStr = ""
For i = Len(str) To 1 Step -1
revStr = revStr & Mid(str, i, 1)
Next i
MsgBox "逆序输出的字符串为:" & revStr'判断一个字符串是否为回文字符串
str = InputBox("请输入一个字符串:")
Dim isPalindrome As Boolean
isPalindrome = True
For i = 1 To Len(str) / 2
If Mid(str, i, 1) <> Mid(str, Len(str) - i + 1, 1) Then
isPalindrome = False
Exit For
End If
Next i
If isPalindrome Then
MsgBox str & "是回文字符串。"
Else
MsgBox str & "不是回文字符串。"
End If