[.NET] select case的問題

看板Visual_Basic作者 (小梁的GG被狗咬啦~( ̄▽))時間15年前 (2009/03/21 15:11), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/1
理想中下面這個程式碼輸入12要等於180,用switch寫的話是: sumofN = sumofN + Microsoft.VisualBasic.Switch(i Mod 6 = 0, i, i Mod 3 = 0, i^ 2, i Mod 2 = 0, i * 2, i, i) 輸入12執行完是180沒錯,但是換成Select Case就寫不太出來,拜託各位教我一下錯在哪 了。 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim N As Integer, errorflag As Boolean = True Try N = CInt(TextBox1.Text) Catch ex As Exception errorflag = False MsgBox("請輸入正確的數值") End Try If errorflag = True Then If N >= 1 And N <= 10000 Then Dim sumofN As Integer = 0 For i As Integer = 1 To N Select Case N Case i Mod 6 = 0 sumofN = sumofN + i Case i Mod 2 = 0 sumofN = sumofN + i * 2 Case i Mod 3 = 0 sumofN = sumofN + i ^ 2 Case Else sumofN = sumofN + i End Select Next Label4.Text = CStr(sumofN) Else MsgBox("請輸入正確的數值(1-10000)") End If End If End Sub -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.170.12.103

03/22 10:47, , 1F
你不需要select case,用多個if寫才是正途
03/22 10:47, 1F
文章代碼(AID): #19n9COn7 (Visual_Basic)