VB编程求1—50的偶数平方和与奇数平方差

来源:百度知道 编辑:UC知道 时间:2024/05/31 04:41:44

dim a as integer
dim b as integer
dim n as integer
dim s1 as integer
dim s2 as integer
for n=a to b
if os(n)=true then
s1=s1+n^2
else
s2=s2+n^2
end if
exit n
print s1-s2

'os(n)
判断是否偶数的你自己写吧·
a 就是第一数,b就是尾数,
你可以任意修改

以下代码可以实现1到N的偶数平方和与奇数平方和之差
'按钮1代码:
Private Sub Command1_Click()
dim min ,max as integer
min=1 '设定参与计算的最小值
max=50 '设定参与计算的最大值
Text1.Text = josc(min,max)
End Sub

'按钮2代码:
Private Sub Command2_Click()
end
End Sub

'josc函数代码
Public Function josc(min as integer,max as integer) as long
Dim js As Double
Dim os As Double
For i = min To max
If i Mod 2 Then
os = os + i ^ 2
Else
js = js + i ^ 2
End If
Next i
josc = os - js
End Function