求高手帮我做一道vb题 最好能用vb2010做
计算出50以内两个数之间(包括开头和结尾的数)所有连续数的和为120的正整数,并在窗体上显示出来,程序输出每组开头和结尾的正整数,并用“~”连接起来,如1~15,这样的数...
计算出50以内两个数之间(包括开头和结尾的数)所有连续数的和为120的正整数,并在窗体上显示出来,程序输出每组开头和结尾的正整数,并用“~”连接起来,如1~15,这样的数有多组,一行输出一组。(提示:二重循环)
展开
2个回答
展开全部
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Dim i, j, k As Integer
Dim sum As Integer
k = 0
Dim g As Graphics = Me.CreateGraphics()
For i = 1 To 50
sum = 0
k += 1
For j = i To 50
sum += j
If sum = 120 Then
g.DrawString(i.ToString & "~" & j.ToString, Me.Font, Brushes.Black, 0, k)
Exit For
ElseIf sum > 120 Then
Exit For
End If
Next
Next
g.Dispose()
End Sub
Dim i, j, k As Integer
Dim sum As Integer
k = 0
Dim g As Graphics = Me.CreateGraphics()
For i = 1 To 50
sum = 0
k += 1
For j = i To 50
sum += j
If sum = 120 Then
g.DrawString(i.ToString & "~" & j.ToString, Me.Font, Brushes.Black, 0, k)
Exit For
ElseIf sum > 120 Then
Exit For
End If
Next
Next
g.Dispose()
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询