看板 Visual_Basic 關於我們 聯絡資訊
我照書上的一個練習題打的,有個地方想請教各位高手: 這個程式由使用者輸入兩個數字,然後按"計算"鈕,如果超過一億,那麼顯示超過一億。 如果沒有超過一億,顯示你剛輸入兩個數字中間偶數的乘積。 EX: 輸入3 及7, 顯示:結果:24 (因為只有4,6相乘) 我的問題如下: 我要判斷為偶數才相乘,書上的寫法是中間加入了if i Mod 2 = 0 then ....End If 我本來寫While i Mod 2 = 0 ....... End While 可是這樣寫,程式就錯了,一樣輸入3,7兩數字,跳出結果是超過一億。 為什麼呀? 有沒有高手幫忙解答一下! 感謝! ============================================================================ Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a, b, i As Integer Dim m As ULong = 1 a = Val(TextBox1.Text) b = Val(TextBox2.Text) For i = a To b If i Mod 2 = 0 Then m = m * i If m > 10 ^ 8 Then Exit For End If Next Label4.Text = "結果:" _ & If(m > 10 ^ 8, "超過一億", Format(m, "#,0")) End Sub End Class -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.168.213.74
petitcuckoo:進去while後 i的數值一直不會變 所以會一直乘上i 11/03 17:34
petitcuckoo:直到 m > 10 ^ 8 才會離開 11/03 17:35
petitcuckoo:若是輸入3跟7 則是一直4*4*4*4*4..直到 > 10 ^ 8 11/03 17:36
fjf1980:原來! 感謝^^ 11/03 18:49