วันจันทร์ที่ 9 สิงหาคม พ.ศ. 2553

ข้อสอบ VB ก๊อปไปวางทับอันเก่าเด้อ

Public Class Form1


    Private Sub code_button1(ByVal num As Integer)

        Button1.Text = "ผลรวมของ 1 ถึง " + num.ToString()

        Dim ans As Integer = 0



        For i As Integer = 1 To num

            ans = ans + i

        Next



        Label3.Text = ans.ToString()



    End Sub



    Private Sub code_button2(ByVal num As Integer)

        Button2.Text = "ผลรวมเฉพาะเลขคี่ของ 1 ถึง " + num.ToString()

        Dim ans As Integer = 0





        For i As Integer = 1 To num

            If (i Mod 2) = 1 Then

                ans = ans + i

            End If

        Next



        Label4.Text = ans.ToString()

    End Sub



    Private Sub code_button3(ByVal num As Integer)

        Button3.Text = "ผลรวมเฉพาะเลขคู่ของ 1 ถึง " + num.ToString()

        Dim ans As Integer = 0



        For i As Integer = 1 To num

            If (i Mod 2) = 0 Then

                ans = ans + i

            End If

        Next



        Label5.Text = ans.ToString()

    End Sub



    Private Sub code_button4(ByVal num As Integer)

        Button4.Text = "!" + num.ToString() + " มีค่าเท่ากับ"

        Dim ans As Integer = factorial(num)



        Label6.Text = ans.ToString()

    End Sub

    Private Sub code_button5(ByVal num As Integer)

        Button5.Text = "หาผลรวมของเลขที่ หาร 3 ลงตัว ตั้งแต่ 1 ถึง " + num.ToString()
        Dim ans As Integer = 0

        For i As Integer = 1 To num
            If (i Mod 3) = 0 Then
                ans += i
            End If
        Next



        Label7.Text = ans.ToString()

    End Sub

    Private Function factorial(ByVal num As Integer) As Integer

        Dim ans As Integer = num



        If (ans <= 0) Then

            Return 1

        End If



        For i As Integer = 1 To num

            ans = ans * i

        Next



        Return (ans)

    End Function



    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim application_number As Integer = 0



        If TextBox1.Text <> "" Then

            application_number = CInt(TextBox1.Text)



            If (application_number > 0) Then

                code_button1(application_number)

                code_button2(application_number)

                code_button3(application_number)

                code_button4(application_number)

                code_button5(application_number)

            End If

        End If

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

    End Sub
End Class

ไม่มีความคิดเห็น:

แสดงความคิดเห็น