vb程序改错题,计算1~1000内的水仙花。。。
计算出1-1000之间的所有“水仙花数”的个数,所谓“水仙花数”是指一个三位数,其各位数字的立方和等于该数本身(如153=1^3+5^3+3^3)。下列程序中的i代表三位...
计算出1-1000之间的所有“水仙花数”的个数,所谓“水仙花数”是指一个三位数,其各位数字的立方和等于该数本身(如 153=1^3+5^3+3^3)。下列程序中的i代表三位数,A、B和C分别代表百位、十位和个位上的三个数字。窗体中有一个“计算”按钮、一个“退出”按钮和一个文本框,窗体运行时,单击“计算”按钮,在文本框中显示出水仙花数的个数,单击“退出”按钮,结束窗体运行。所用控件的属性都已设置好,只是为“计算”按钮编写的代码中有2个错误进行修改。不准添加或删除原程序代码行
Private Sub Command1_Click()
Dim n As Integer, i As Integer, s As String
Dim A As Integer, B As Integer, C As Integer
n = 1
For i = 100 To 999
s = Trim(Str$(i))
A = Val(Left(s, 1))
B = Val(Mid(s, 2, 1))
C = Val(Right(s, 1))
If i = A ^ 3 + B ^ 3 + C ^ 3 Then
n = n + 1
End If
Next i
Text1 = s
End Sub
Private Sub Command2_Click()
End
End Sub 展开
Private Sub Command1_Click()
Dim n As Integer, i As Integer, s As String
Dim A As Integer, B As Integer, C As Integer
n = 1
For i = 100 To 999
s = Trim(Str$(i))
A = Val(Left(s, 1))
B = Val(Mid(s, 2, 1))
C = Val(Right(s, 1))
If i = A ^ 3 + B ^ 3 + C ^ 3 Then
n = n + 1
End If
Next i
Text1 = s
End Sub
Private Sub Command2_Click()
End
End Sub 展开
2个回答
展开全部
Private Sub Command1_Click()
Dim n As Integer, i As Integer, s As String
Dim A As Integer, B As Integer, C As Integer
n = 0 '这个地方n是统计水仙花数的个数的变量,开始时应为0,而不是1.
For i = 100 To 999
s = Trim(Str$(i))
A = Val(Left(s, 1))
B = Val(Mid(s, 2, 1))
C = Val(Right(s, 1))
If i = A ^ 3 + B ^ 3 + C ^ 3 Then
n = n + 1
End If
Next i
Text1 = n '这个地方文本框应该输出的是水仙花数的个数n,而不应该是s。 ‘
End Sub
两个地方改好了,请采纳哦!!!
Dim n As Integer, i As Integer, s As String
Dim A As Integer, B As Integer, C As Integer
n = 0 '这个地方n是统计水仙花数的个数的变量,开始时应为0,而不是1.
For i = 100 To 999
s = Trim(Str$(i))
A = Val(Left(s, 1))
B = Val(Mid(s, 2, 1))
C = Val(Right(s, 1))
If i = A ^ 3 + B ^ 3 + C ^ 3 Then
n = n + 1
End If
Next i
Text1 = n '这个地方文本框应该输出的是水仙花数的个数n,而不应该是s。 ‘
End Sub
两个地方改好了,请采纳哦!!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询