用VB求1到N的平方和,其中N的值由自己给定。要求使用for....next 语句
PrivateSubCommand1_Click()ClsDimsum(1ton)Asdouble,n%,i%n=Val(Text1.Text)sum(1)=1Fori=...
Private Sub Command1_Click()
Cls
Dim sum(1 to n) As double, n%, i%
n = Val(Text1.Text)
sum(1) = 1
For i = 1 To n
sum(i) = sum(i) + i ^ 2 - 1
Next i
MsgBox "结果是:" & sum(n)
End Sub
这是我自己写的
运行时的一个出错提示是:要求常数表达式
这应该怎么去改。
请高手指教! 展开
Cls
Dim sum(1 to n) As double, n%, i%
n = Val(Text1.Text)
sum(1) = 1
For i = 1 To n
sum(i) = sum(i) + i ^ 2 - 1
Next i
MsgBox "结果是:" & sum(n)
End Sub
这是我自己写的
运行时的一个出错提示是:要求常数表达式
这应该怎么去改。
请高手指教! 展开
展开全部
错误的原因是:定义数组要用常量 如dim sum(1 to 100),这里1和100都是常量,不能用变量来定义数组。
Private Sub Command1_Click()
cls
Dim i, n As Integer
Dim Sum As Double
Sum = 0
n = Val(Text1.Text)
For i = 1 To n
Sum = Sum + i ^ 2
Next i
MsgBox "结果是:" & Sum, 64, "结果"
End Sub
Private Sub Command1_Click()
cls
Dim i, n As Integer
Dim Sum As Double
Sum = 0
n = Val(Text1.Text)
For i = 1 To n
Sum = Sum + i ^ 2
Next i
MsgBox "结果是:" & Sum, 64, "结果"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Cls
Dim sum() As double, n%, i%
n = Val(Text1.Text)
ReDim sum(n)
For i = 1 To n
sum(i) = sum(i) + i ^ 2 - 1
Next i
MsgBox "结果是:" & sum(n)
End Sub
Cls
Dim sum() As double, n%, i%
n = Val(Text1.Text)
ReDim sum(n)
For i = 1 To n
sum(i) = sum(i) + i ^ 2 - 1
Next i
MsgBox "结果是:" & sum(n)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Cls
Dim sum As double, n as integer, i as integer
n = Val(Text1.Text)
sum= 0
For i = 1 To n
sum = sum + cdbl(i) ^ 2
Next i
MsgBox "结果是:" & sum
End Sub
Cls
Dim sum As double, n as integer, i as integer
n = Val(Text1.Text)
sum= 0
For i = 1 To n
sum = sum + cdbl(i) ^ 2
Next i
MsgBox "结果是:" & sum
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Cls
Dim n%, i% ,k%
n = Val(Text1.Text)
k=0
For i = 1 To n
k = k + i ^ 2 - 1
Next i
MsgBox "结果是:" & k
End Sub
Cls
Dim n%, i% ,k%
n = Val(Text1.Text)
k=0
For i = 1 To n
k = k + i ^ 2 - 1
Next i
MsgBox "结果是:" & k
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询