[ VB2010 ] - 限定條件重複流程003
●計算1+2+3+...+998+999+1000的總和:
●計算1+4+7+...+94+97+100的總和
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim I As Integer = 0
Dim S As Integer = 0
TextBox1.Text = ""
TextBox1.Text = "●計算1+2+3+...+998+999+1000的總和:" & Chr(13) + Chr(10)
I = 1
S = 0
Do Until I > 1000
S = S + I
I += 1
Loop
TextBox1.Text = TextBox1.Text & "1. 結果:" & S & Chr(13) + Chr(10)
I = 1
S = 0
Do
S = S + I
I += 1
Loop Until I > 1000
TextBox1.Text = TextBox1.Text & "2. 結果:" & S & Chr(13) + Chr(10)
I = 1
S = 0
While I <= 1000
S = S + I
I += 1
End While
TextBox1.Text = TextBox1.Text & "3. 結果:" & S & Chr(13) + Chr(10)
I = 1
S = 0
Do
S = S + I
I += 1
If I > 1000 Then
Exit Do
End If
Loop
TextBox1.Text = TextBox1.Text & "4. 結果:" & S & Chr(13) + Chr(10)
I = 1
S = 0
While True
S = S + I
I += 1
If I > 1000 Then
Exit While
End If
End While
TextBox1.Text = TextBox1.Text & "5. 結果:" & S & Chr(13) + Chr(10)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim I As Integer = 0
Dim S As Integer = 0
I = 1
S = 0
TextBox1.Text = ""
TextBox1.Text = "●計算1+4+7+...+94+97+100的總和 " & Chr(13) + Chr(10)
Do Until I > 100
S = S + I
I += 3
Loop
TextBox1.Text = TextBox1.Text & "1. 結果:" & S & Chr(13) + Chr(10)
I = 1
S = 0
Do
S = S + I
I += 3
Loop Until I > 100
TextBox1.Text = TextBox1.Text & "2. 結果:" & S & Chr(13) + Chr(10)
I = 1
S = 0
While I <= 100
S = S + I
I += 3
End While
TextBox1.Text = TextBox1.Text & "3. 結果:" & S & Chr(13) + Chr(10)
I = 1
S = 0
Do
S = S + I
I += 3
If I > 100 Then
Exit Do
End If
Loop
TextBox1.Text = TextBox1.Text & "4. 結果:" & S & Chr(13) + Chr(10)
I = 1
S = 0
While True
S = S + I
I += 3
If I > 100 Then
Exit While
End If
End While
TextBox1.Text = TextBox1.Text & "5. 結果:" & S & Chr(13) + Chr(10)
End Sub
End Class
沒有留言:
張貼留言