2009年3月31日 星期二

摩天輪基礎

image

image

'摩天輪支架
'摩天輪連接纜繩
'摩天輪轉動動作

Public Class Form1
    Dim x2, y2 As Single
    Dim angle
    Dim anglePi
    Dim r
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Me.Width = 600
        Me.Height = 600
        Me.BackColor = Color.White

        '摩天輪支架
        e.Graphics.DrawLine(Pens.Black, 300, 250, 300 + 100, 550)
        e.Graphics.DrawLine(Pens.Black, 300, 250, 300 - 100, 550)
        e.Graphics.DrawLine(Pens.Black, 300 - 83, 500, 300 + 83, 500)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim g As Graphics = Me.CreateGraphics
        g.DrawLine(Pens.White, 300, 250, x2, y2)
        anglePi = angle * 3.14159 / 180
        x2 = 300 + r * Math.Cos(anglePi)
        y2 = 250 + r * Math.Sin(anglePi)

        '摩天輪連接纜繩
        g.DrawLine(Pens.Black, 300, 250, x2, y2)
        angle = angle + 15
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Timer1.Interval = 100

        r = 200
        angle = 0
    End Sub
End Class

2009年3月19日 星期四

亂數應用 - 樂透開獎與撲克牌發牌

image

image

image image

Public Class Form1
    Dim i, n, str1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
        'Int(Rnd() * 個數) + 最小值

        '樂透開獎 - 會重覆待修正版
        str1 = ""
        For i = 1 To 7
            n = Int(Rnd() * (42 - 1 + 1)) + 1
            str1 = str1 & n & "  "
        Next i
        MsgBox(str1)

        '樂透開獎 - 不重覆版
        Dim a(42)
        For i = 1 To 42
            a(i) = i
        Next
        For i = 1 To 42
            n = Int(Rnd() * (42 - 1 + 1)) + 1
            Dim t = a(i)
            a(i) = a(n)
            a(n) = t
        Next
        str1 = ""
        For i = 1 To 7
            str1 = str1 & a(i) & "  "
        Next
        MsgBox(str1)

        '撲克牌準備
        Dim b(52, 2)
        For i = 1 To 52
            b(i, 1) = (i - 1) Mod 13 + 1
            If i Mod 4 = 0 Then b(i, 2) = "黑桃"
            If i Mod 4 = 1 Then b(i, 2) = "紅心"
            If i Mod 4 = 2 Then b(i, 2) = "鑽石"
            If i Mod 4 = 3 Then b(i, 2) = "梅花"
        Next
        str1 = ""
        For i = 1 To 52
            str1 = str1 & b(i, 1) & "-" & b(i, 2) & vbNewLine
        Next
        MsgBox(str1)

        '撲克牌洗牌
        For i = 1 To 52
            n = Int(Rnd() * (42 - 1 + 1)) + 1
            Dim t = b(i, 1)
            b(i, 1) = b(n, 1)
            b(n, 1) = t

            t = b(i, 2)
            b(i, 2) = b(n, 2)
            b(n, 2) = t
        Next
        str1 = ""
        For i = 1 To 52
            str1 = str1 & b(i, 1) & "-" & b(i, 2) & vbNewLine
        Next
        MsgBox(str1)
        End
    End Sub
End Class

2009年3月13日 星期五

將兩數相乘

 

9*9=81             image 

 

 

 

6*5=30              image

 

 

 

 

Public Class Form1
    '將任意兩數數字相加
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text)
    End Sub

End Class

將任意兩數相加。

image

image

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
    End Sub
End Class

以前校內程式設計比賽的題目-第四題

image

image

image

Public Class Form1
    Dim a, b, c, a1, a2 As Integer
    Dim a3, a4 As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        a3 = ""
        a4 = ""
        a1 = TextBox1.Text
        a2 = TextBox2.Text
        For a = Len(a1) To 1 Step -1
            a3 = a3 & Mid(a1, a, 1)
        Next
        For b = Len(a2) To 1 Step -1
            a4 = a4 & Mid(a2, b, 1)
        Next
        c = Val(a3) + Val(a4)
        TextBox3.Text = c
    End Sub
End Class

將任意兩數相乘的計算結果

image

'將任意兩數相乘計算結果
Public Class Form1
    Dim a, b As Long
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        a = Val(TextBox1.Text)
        b = Val(TextBox2.Text)
        TextBox3.Text = a * b
    End Sub
End Class

麗山50-4

 

image 

image 

image

Public Class Form1
    Dim i, a, b
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If a Mod 9 <> 0 Then
            b = "此數不被九整除"
        Else
            b = "此數可被九整除"
        End If
        a = Val(TextBox1.Text)
        Label1.Text = b

    End Sub
End Class

以前校內程式設計比賽的題目-第二題

image

image

image

Public Class Form1
    Dim a, s As Integer
    Dim v As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        a = InputBox("請輸入N值:")
        If a <= 100 And a >= 10 Then
            For s = 1 To a
                If s Mod 7 = 1 Then
                    v = v & s & " "
                End If
            Next
        End If
        TextBox1.Text = v
    End Sub
End Class

麗山50-11

image

 

image

 

Public Class Form1
    Dim b As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For A As Integer = 1 To 9999
            If (A Mod 7 = 2) And (A Mod 9 = 2) And (A Mod 3 = 2) Then
                b = b & A & vbNewLine
            End If
        Next
        TextBox1.Text = b
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class

 

^^

將任意兩數相加。

image

 

Public Class Form1
    '將任意兩數數字相加
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
    End Sub

End Class

印出兩個 N * N 矩陣的乘積。

image

image

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a, b
        a = Val(TextBox1.Text)
        b = Val(TextBox2.Text)
        TextBox3.Text = a * b
    End Sub
End Class

計算 1 + 2 + ... + n =?

 image

image

image

 

Module Module1

    Sub Main()
        Dim a As Long
        Dim N As Long
        N = InputBox("請輸入N值")
        For i As Long = 1 To N
            a = a + i
        Next
        Console.WriteLine(a)
        Console.ReadLine()
    End Sub

End Module

麗山50-4

image

image

 

Public Class Form1
    Dim i, a, b
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If a Mod 9 <> 0 Then
            b = "此數不被9整除"
        Else
            b = "此數可被9整除"
        End If
        a = Val(TextBox1.Text)
        Label1.Text = b
    End Sub
End Class

試使用InputBox()函式分別輸入你的操行和學期成績,再透過If巢狀迴圈判斷只要操行成績80分以上(含)為第一條件;第二條件學科成績至少80分以上(含),若學科成績大於等於80分,獎學金1,000元;學科成績大於等於90分,則獎學金2,000 元。再透過MsgBox()函式顯示操行和學科成績和所得獎學金的金額。

 

clip_image002

 

 

clip_image002[4]

 

 

clip_image002[6]

    操行84,學科70

 

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conduct, score As Integer
        conduct = InputBox("請輸入操行成績:", "操行成績")
        score = InputBox("請輸入學期成績", "學期成績")
        If conduct >= 80 Then
            If score >= 80 Then
                If score >= 90 Then
                    MsgBox("操行" & conduct & "分,學科" & score & "分,獎學金 2,000元")
                Else
                    MsgBox("操行" & conduct & "分,學科" & score & "分,獎學金 1,000元")
                End If
            Else
                MsgBox("學科" & score & "分,未達申請條件")
            End If
            MsgBox("操行" & conduct & "分,未達申請條件")
        End If
        End
    End Sub
End Class

井字遊戲 -- 二種電腦邏輯對戰版

image

image

Public Class Form1
    Dim btn(9) As Button
    Dim i
    Dim j
    Dim sco(9)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
        btn(1) = Button1
        btn(2) = Button2
        btn(3) = Button3
        btn(4) = Button4
        btn(5) = Button5
        btn(6) = Button6
        btn(7) = Button7
        btn(8) = Button8
        btn(9) = Button9
        For i = 1 To 9
            With btn(i)
                .Height = btn(i).Width
                .Font = New Font("Arial", 50)
                ' .Text = Mid(btn(i).Text, Len(btn(i).Text), 1)
                .Text = ""
                .Left = 10 + btn(1).Width * ((i - 1) Mod 3)
                .Top = 10 + btn(1).Height * ((i - 1) \ 3)
            End With
            sco(i) = 0
        Next
        Label1.Top = btn(1).Height * 3 + 20
        Label1.Text = ""
        Button10.Top = btn(1).Height * 3 + 20
        Button11.Top = btn(1).Height * 3 + 20
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        j = 1
        While btn(j).Text <> ""
            j = j + 1
        End While
        btn(j).Text = "○"
        sco(j) = 1
        Call checkwin()
    End Sub
    Sub checkwin()
        '○贏了
        If sco(1) * sco(2) * sco(3) = 1 Then Label1.Text = "○贏了"
        If sco(4) * sco(5) * sco(6) = 1 Then Label1.Text = "○贏了"
        If sco(7) * sco(8) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(4) * sco(7) = 1 Then Label1.Text = "○贏了!"
        If sco(2) * sco(5) * sco(8) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(6) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(5) * sco(9) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(5) * sco(7) = 1 Then Label1.Text = "○贏了!"

        '×贏了!
        If sco(1) * sco(2) * sco(3) = 8 Then Label1.Text = "×贏了!"
        If sco(4) * sco(5) * sco(6) = 8 Then Label1.Text = "×贏了!"
        If sco(7) * sco(8) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(4) * sco(7) = 8 Then Label1.Text = "×贏了!"
        If sco(2) * sco(5) * sco(8) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(6) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(5) * sco(9) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(5) * sco(7) = 8 Then Label1.Text = "×贏了!"
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        j = Int(Rnd() * 9) + 1
        While btn(j).Text <> ""
            j = Int(Rnd() * 9) + 1
        End While
        btn(j).Text = "×"
        sco(j) = 2

        Call checkwin()

    End Sub
End Class

將秒數轉為時間

image

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '將秒數轉為時間
        Dim y = 10000
        Dim sec = y Mod 60
        Dim minute = y \ 60 Mod 60
        Dim hour = y \ 3600
        MsgBox(hour & ":" & minute & ":" & sec)
    End Sub
End Class

井字遊戲 -- 二種電腦邏輯對戰版

image image

 

 

 

 

Public Class Form1
    Dim but(9) As Button
    Dim i
    Dim sco(9)
    Dim j
    Dim no

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call Hypothesis()
    End Sub

    Sub Hypothesis()

        but(1) = Button1
        but(2) = Button2
        but(3) = Button3
        but(4) = Button4
        but(5) = Button5
        but(6) = Button6
        but(7) = Button7
        but(8) = Button8
        but(9) = Button9

        For i = 1 To 9
            With but(i)
                .Height = but(i).Width
                .Font = New Font("Arial", 50)
                .Text = Mid(but(i).Text, Len(but(i).Text), 1)
                .Text = ""
                .Left = 10 + but(1).Width * ((i - 1) Mod 3)
                .Top = 10 + but(1).Height * ((i - 1) \ 3)
            End With

            sco(i) = 0
        Next

        With Label1
            .Top = but(1).Height * 3 + 20
            Label1.Left = 10
            Label1.Text = ""
            .Font = New Font("標楷體", 15)
        End With

        With Button10
            .Top = but(1).Height * 3 + 15
            .Font = New Font("標楷體", 10)
            .Left = but(1).Width * 2.1
            .Text = " × "
        End With

        With Button11
            .Left = but(1).Width * 1
            .Top = but(1).Height * 3 + 15
            .Font = New Font("標楷體", 10)
            .Text = " ○ "
        End With

    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
        no = Mid(CType(sender, Button).Name, Len(CType(sender, Button).Name), 1)

    End Sub
    Sub play1()
        j = 2
        While but(j).Text <> ""
            j = Int(Rnd() * 9) + 1
        End While
        but(j).Text = "○"
        sco(j) = 1

    End Sub
    Sub play2()
        j = 1
        While but(j).Text <> ""
            j = Int(Rnd() * 9) + 1
        End While
        but(j).Text = "×"
        sco(j) = 2

    End Sub
    Sub play()

        If sco(1) * sco(2) * sco(3) = 1 Then Label1.Text = "○ 贏了!"
        If sco(4) * sco(5) * sco(6) = 1 Then Label1.Text = "○ 贏了!"
        If sco(7) * sco(8) * sco(9) = 1 Then Label1.Text = "○ 贏了!"
        If sco(1) * sco(4) * sco(7) = 1 Then Label1.Text = "○ 贏了!"
        If sco(2) * sco(5) * sco(8) = 1 Then Label1.Text = "○ 贏了!"
        If sco(3) * sco(6) * sco(9) = 1 Then Label1.Text = "○ 贏了!"
        If sco(1) * sco(5) * sco(9) = 1 Then Label1.Text = "○ 贏了!"
        If sco(3) * sco(5) * sco(7) = 1 Then Label1.Text = "○ 贏了!"

        If sco(1) * sco(2) * sco(3) = 8 Then Label1.Text = "× 贏了!"
        If sco(4) * sco(5) * sco(6) = 8 Then Label1.Text = "× 贏了!"
        If sco(7) * sco(8) * sco(9) = 8 Then Label1.Text = "× 贏了!"
        If sco(1) * sco(4) * sco(7) = 8 Then Label1.Text = "× 贏了!"
        If sco(2) * sco(5) * sco(8) = 8 Then Label1.Text = "× 贏了!"
        If sco(3) * sco(6) * sco(9) = 8 Then Label1.Text = "× 贏了!"
        If sco(1) * sco(5) * sco(9) = 8 Then Label1.Text = "× 贏了!"
        If sco(3) * sco(5) * sco(7) = 8 Then Label1.Text = "× 贏了!"
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        play2()
        play()
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        play1()
        play()
    End Sub
End Class

2009年3月12日 星期四

井字遊戲 -- 二種電腦邏輯對戰版

image

'二種電腦邏輯對戰版
'○採用由第1至第9格,依序找一個可以下的格子就下的方法
'×採用由第1至第9格,隨機方式找一個可以下的格子就下的方法
Public Class Form1
    Dim btn(9) As Button
    Dim i
    Dim j
    Dim sco(9)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
        btn(1) = Button1
        btn(2) = Button2
        btn(3) = Button3
        btn(4) = Button4
        btn(5) = Button5
        btn(6) = Button6
        btn(7) = Button7
        btn(8) = Button8
        btn(9) = Button9
        For i = 1 To 9
            With btn(i)
                .Height = btn(i).Width
                .Font = New Font("Arial", 50)
                ' .Text = Mid(btn(i).Text, Len(btn(i).Text), 1)
                .Text = ""
                .Left = 10 + btn(1).Width * ((i - 1) Mod 3)
                .Top = 10 + btn(1).Height * ((i - 1) \ 3)
            End With
            sco(i) = 0
        Next
        Label1.Top = btn(1).Height * 3 + 20
        Label1.Text = ""
        Button10.Top = btn(1).Height * 3 + 20
        Button11.Top = btn(1).Height * 3 + 20
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        j = 1
        While btn(j).Text <> ""
            j = j + 1
        End While
        btn(j).Text = "○"
        sco(j) = 1

        Call checkWin()

    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        j = Int(Rnd() * 9) + 1
        While btn(j).Text <> ""
            j = Int(Rnd() * 9) + 1
        End While
        btn(j).Text = "×"
        sco(j) = 2

        Call checkWin()
    End Sub

    Sub checkWin()
        '○贏了!
        If sco(1) * sco(2) * sco(3) = 1 Then Label1.Text = "○贏了!"
        If sco(4) * sco(5) * sco(6) = 1 Then Label1.Text = "○贏了!"
        If sco(7) * sco(8) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(4) * sco(7) = 1 Then Label1.Text = "○贏了!"
        If sco(2) * sco(5) * sco(8) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(6) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(5) * sco(9) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(5) * sco(7) = 1 Then Label1.Text = "○贏了!"

        '×贏了!
        If sco(1) * sco(2) * sco(3) = 8 Then Label1.Text = "×贏了!"
        If sco(4) * sco(5) * sco(6) = 8 Then Label1.Text = "×贏了!"
        If sco(7) * sco(8) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(4) * sco(7) = 8 Then Label1.Text = "×贏了!"
        If sco(2) * sco(5) * sco(8) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(6) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(5) * sco(9) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(5) * sco(7) = 8 Then Label1.Text = "×贏了!"
    End Sub
End Class

2009年3月11日 星期三

井宇遊戲 - 人與電腦對戰版 2008/03/11

image

'井宇遊戲 - 人與電腦對戰版
' 電腦每次用亂數法,下一個尚未被使用的格子
Public Class Form1
    Dim btn(9) As Button, i As Integer
    Dim oxFlag As Boolean
    Dim sco(9) As Integer
    Dim no As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call prepare()

        no = Int(Rnd() * 9) + 1
        Call playOne()
    End Sub

    Sub prepare()
        Randomize()
        oxFlag = True
        btn(1) = Button1
        btn(2) = Button2
        btn(3) = Button3
        btn(4) = Button4
        btn(5) = Button5
        btn(6) = Button6
        btn(7) = Button7
        btn(8) = Button8
        btn(9) = Button9

        For i = 1 To 9
            With btn(i)
                .Text = ""
                .Height = .Width
                .Font = New Font("Arial", 50)
                .Left = 10 + .Width * ((i - 1) Mod 3)
                .Top = 10 + .Height * ((i - 1) \ 3)
            End With
            sco(i) = 0
        Next

        Me.Width = 3 * btn(1).Width + 30
        With Label1
            .Top = 3 * btn(1).Height + 30
            .Font = New Font("新細明體", 18)
            .Text = ""
            .ForeColor = Color.Red
        End With

        Button10.Top = 3 * btn(1).Height + 30

        Me.Height = 3 * btn(1).Height + 100
        Button10.Left = Me.Width - Button10.Width - 20
        Button10.Text = "重新開始"
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
        no = Mid(CType(sender, Button).Name, Len(CType(sender, Button).Name), 1)
        Call playOne()

        '換電腦玩 - 抓一個空的格子 - 用亂數的方法
        Dim j = Int(Rnd() * 9) + 1
        While sco(j) <> 0
            j = Int(Rnd() * 9) + 1
        End While

        no = j
        Call playOne()
    End Sub

    Sub playOne()
        If oxFlag = True Then
            btn(no).Text = "○"
            sco(no) = 1
        Else
            btn(no).Text = "×"
            sco(no) = 2
        End If
        btn(no).Enabled = False
        oxFlag = Not oxFlag

        '○贏了!
        If sco(1) * sco(2) * sco(3) = 1 Then Label1.Text = "○贏了!"
        If sco(4) * sco(5) * sco(6) = 1 Then Label1.Text = "○贏了!"
        If sco(7) * sco(8) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(4) * sco(7) = 1 Then Label1.Text = "○贏了!"
        If sco(2) * sco(5) * sco(8) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(6) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(5) * sco(9) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(5) * sco(7) = 1 Then Label1.Text = "○贏了!"

        '×贏了!
        If sco(1) * sco(2) * sco(3) = 8 Then Label1.Text = "×贏了!"
        If sco(4) * sco(5) * sco(6) = 8 Then Label1.Text = "×贏了!"
        If sco(7) * sco(8) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(4) * sco(7) = 8 Then Label1.Text = "×贏了!"
        If sco(2) * sco(5) * sco(8) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(6) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(5) * sco(9) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(5) * sco(7) = 8 Then Label1.Text = "×贏了!"
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        For i = 1 To 9
            btn(i).Text = ""
            sco(i) = 0
            btn(i).Enabled = True
        Next
        Label1.Text = ""
    End Sub
End Class

2009年3月10日 星期二

井字遊戲--人機互動版-20090310版

'井字遊戲--人機互動版-20090310版

image

Public Class Form1
    Dim btn(9) As Button, i As Integer
    Dim oxFlag As Boolean
    Dim sco(9) As Integer
    Dim j As Integer
    Dim no As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call prepare()
        cplay()
    End Sub
    Sub cplay()
        Call getOneN()
        Call proc()
    End Sub


    Sub getOneN()  '取得一個未有人下過的空格
        j = Int(Rnd() * 9) + 1
        While sco(j) <> 0
            j = Int(Rnd() * 9) + 1
        End While
        no = j
    End Sub
    Sub prepare()
        Randomize()
        oxFlag = True
        btn(1) = Button1
        btn(2) = Button2
        btn(3) = Button3
        btn(4) = Button4
        btn(5) = Button5
        btn(6) = Button6
        btn(7) = Button7
        btn(8) = Button8
        btn(9) = Button9

        For i = 1 To 9
            With btn(i)
                '.Text = Mid((btn(i).Name), Len((btn(i).Name)), 1)
                .Text = ""
                .Height = .Width
                .Font = New Font("Arial", 50)
                .Left = 10 + .Width * ((i - 1) Mod 3)
                .Top = 10 + .Height * ((i - 1) \ 3)
            End With
            sco(i) = 0
        Next

        Me.Width = 3 * btn(1).Width + 30
        With Label1
            .Text = ""
            .Top = 3 * btn(1).Height + 30
            .Font = New Font("新細明體", 16)
            .ForeColor = Color.Red
        End With

        Button10.Top = 3 * btn(1).Height + 30

        Me.Height = 3 * btn(1).Height + 100
        Button10.Left = Me.Width - Button10.Width - 20
        Button10.Text = "重新開始"
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
        no = Mid(CType(sender, Button).Name, Len(CType(sender, Button).Name), 1)
        Call proc()
        Call cplay()
    End Sub
    Sub proc()
        If oxFlag = True Then
            btn(no).Text = "○"
            sco(no) = 1
        Else
            btn(no).Text = "×"
            sco(no) = 2
        End If
        btn(no).Enabled = False

        '○
        If sco(1) * sco(2) * sco(3) = 1 Then Label1.Text = "○贏了!"
        If sco(4) * sco(5) * sco(6) = 1 Then Label1.Text = "○贏了!"
        If sco(7) * sco(8) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(4) * sco(7) = 1 Then Label1.Text = "○贏了!"
        If sco(2) * sco(5) * sco(8) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(6) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(5) * sco(9) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(5) * sco(7) = 1 Then Label1.Text = "○贏了!"

        '×
        If sco(1) * sco(2) * sco(3) = 8 Then Label1.Text = "×贏了!"
        If sco(4) * sco(5) * sco(6) = 8 Then Label1.Text = "×贏了!"
        If sco(7) * sco(8) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(4) * sco(7) = 8 Then Label1.Text = "×贏了!"
        If sco(2) * sco(5) * sco(8) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(6) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(5) * sco(9) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(5) * sco(7) = 8 Then Label1.Text = "×贏了!"

        oxFlag = Not oxFlag
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        For i = 1 To 9
            btn(i).Enabled = True
            btn(i).Text = ""
            sco(i) = 0
        Next
        Label1.Text = ""
        Call cplay()
    End Sub
End Class

井字遊戲20090310版

image

'井字遊戲20090310版

Public Class Form1
    Dim btn(9) As Button, i As Integer
    Dim oxFlag As Boolean
    Dim sco(9) As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oxFlag = True
        btn(1) = Button1
        btn(2) = Button2
        btn(3) = Button3
        btn(4) = Button4
        btn(5) = Button5
        btn(6) = Button6
        btn(7) = Button7
        btn(8) = Button8
        btn(9) = Button9

        For i = 1 To 9
            With btn(i)
                '.Text = Mid((btn(i).Name), Len((btn(i).Name)), 1)
                .Text = ""
                .Height = .Width
                .Font = New Font("Arial", 50)
                .Left = 10 + .Width * ((i - 1) Mod 3)
                .Top = 10 + .Height * ((i - 1) \ 3)
            End With
            sco(i) = 0
        Next

        Me.Width = 3 * btn(1).Width + 30
        With Label1
            .Text = ""
            .Top = 3 * btn(1).Height + 30
            .Font = New Font("新細明體", 16)
            .ForeColor = Color.Red
        End With

        Button10.Top = 3 * btn(1).Height + 30

        Me.Height = 3 * btn(1).Height + 100
        Button10.Left = Me.Width - Button10.Width - 20
        Button10.Text = "重新開始"
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
        Dim no = Mid(CType(sender, Button).Name, Len(CType(sender, Button).Name), 1)
        If oxFlag = True Then
            btn(no).Text = "○"
            sco(no) = 1
        Else
            btn(no).Text = "×"
            sco(no) = 2
        End If
        btn(no).Enabled = False

        '○
        If sco(1) * sco(2) * sco(3) = 1 Then Label1.Text = "○贏了!"
        If sco(4) * sco(5) * sco(6) = 1 Then Label1.Text = "○贏了!"
        If sco(7) * sco(8) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(4) * sco(7) = 1 Then Label1.Text = "○贏了!"
        If sco(2) * sco(5) * sco(8) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(6) * sco(9) = 1 Then Label1.Text = "○贏了!"

        If sco(1) * sco(5) * sco(9) = 1 Then Label1.Text = "○贏了!"
        If sco(3) * sco(5) * sco(7) = 1 Then Label1.Text = "○贏了!"

        '×
        If sco(1) * sco(2) * sco(3) = 8 Then Label1.Text = "×贏了!"
        If sco(4) * sco(5) * sco(6) = 8 Then Label1.Text = "×贏了!"
        If sco(7) * sco(8) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(4) * sco(7) = 8 Then Label1.Text = "×贏了!"
        If sco(2) * sco(5) * sco(8) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(6) * sco(9) = 8 Then Label1.Text = "×贏了!"

        If sco(1) * sco(5) * sco(9) = 8 Then Label1.Text = "×贏了!"
        If sco(3) * sco(5) * sco(7) = 8 Then Label1.Text = "×贏了!"

        oxFlag = Not oxFlag
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        For i = 1 To 9
            btn(i).Enabled = True
            btn(i).Text = ""
            sco(i) = 0
        Next
        Label1.Text = ""
    End Sub
End Class

2009年3月9日 星期一

井字遊戲 2009/03/09版

image

image

'2009/03/09版

Public Class Form1
    Dim btn(9) As Button, i As Integer
    Dim oxFlag As Boolean
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oxFlag = True
        btn(1) = Button1
        btn(2) = Button2
        btn(3) = Button3
        btn(4) = Button4
        btn(5) = Button5
        btn(6) = Button6
        btn(7) = Button7
        btn(8) = Button8
        btn(9) = Button9

        For i = 1 To 9
            With btn(i)
                .Text = Mid((btn(i).Name), Len((btn(i).Name)), 1)
                .Height = .Width
                .Font = New Font("Arial", 20)
                .Left = 10 + .Width * ((i - 1) Mod 3)
                .Top = 10 + .Height * ((i - 1) \ 3)
            End With
        Next

        Me.Width = 3 * btn(1).Width + 30
        Label1.Top = 3 * btn(1).Height + 30

        Button10.Top = 3 * btn(1).Height + 30

        Me.Height = 3 * btn(1).Height + 100
        Button10.Left = Me.Width - Button10.Width - 20
        Button10.Text = "重新開始"
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
        Dim no = Mid(CType(sender, Button).Name, Len(CType(sender, Button).Name), 1)
        If oxFlag = True Then
            btn(no).Text = "○"
        Else
            btn(no).Text = "×"
        End If
        oxFlag = Not oxFlag
    End Sub
End Class

2009年3月5日 星期四

99-14 印出兩個 N * N 矩陣的乘積。

 

image 

Public Class Form1
    Dim a, b
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        a = Val(TextBox1.Text)
        b = Val(TextBox2.Text)
        TextBox3.Text = a * b

    End Sub
End Class

○×遊戲 - 雛型版

image

'○×遊戲 - 雛型版
Public Class Form1
    Dim btn(9) As Button
    Dim i
    Dim oxFlag As Boolean
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oxFlag = True
        btn(1) = Button1
        btn(2) = Button2
        btn(3) = Button3
        btn(4) = Button4
        btn(5) = Button5
        btn(6) = Button6
        btn(7) = Button7
        btn(8) = Button8
        btn(9) = Button9

        For i = 1 To 9
            With btn(i)
                .Height = .Width
                .Text = ""
                .Font = New Font("新細明體", 50)
                .Left = 15 + ((i - 1) Mod 3) * .Width
                .Top = 15 + ((i - 1) \ 3) * .Height
            End With
        Next
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
        If oxFlag = True Then
            CType(sender, Button).Text = "○"
        Else
            CType(sender, Button).Text = "×"
        End If
        If Button1.Text = "○" And Button2.Text = "○" And Button3.Text = "○" Then Label1.Text = "○贏了!"
        If Button4.Text = "○" And Button5.Text = "○" And Button6.Text = "○" Then Label1.Text = "○贏了!"
        If Button7.Text = "○" And Button8.Text = "○" And Button9.Text = "○" Then Label1.Text = "○贏了!"

        If Button1.Text = "○" And Button4.Text = "○" And Button7.Text = "○" Then Label1.Text = "○贏了!"
        If Button2.Text = "○" And Button5.Text = "○" And Button8.Text = "○" Then Label1.Text = "○贏了!"
        If Button3.Text = "○" And Button6.Text = "○" And Button9.Text = "○" Then Label1.Text = "○贏了!"

        If Button1.Text = "○" And Button5.Text = "○" And Button9.Text = "○" Then Label1.Text = "○贏了!"
        If Button3.Text = "○" And Button5.Text = "○" And Button7.Text = "○" Then Label1.Text = "○贏了!"

        If Button1.Text = "×" And Button2.Text = "×" And Button3.Text = "×" Then Label1.Text = "×贏了!"
        If Button4.Text = "×" And Button5.Text = "×" And Button6.Text = "×" Then Label1.Text = "×贏了!"
        If Button7.Text = "×" And Button8.Text = "×" And Button9.Text = "×" Then Label1.Text = "×贏了!"

        If Button1.Text = "×" And Button4.Text = "×" And Button7.Text = "×" Then Label1.Text = "×贏了!"
        If Button2.Text = "×" And Button5.Text = "×" And Button8.Text = "×" Then Label1.Text = "×贏了!"
        If Button3.Text = "×" And Button6.Text = "×" And Button9.Text = "×" Then Label1.Text = "×贏了!"

        If Button1.Text = "×" And Button5.Text = "×" And Button9.Text = "×" Then Label1.Text = "×贏了!"
        If Button3.Text = "×" And Button5.Text = "×" And Button7.Text = "×" Then Label1.Text = "×贏了!"

        oxFlag = Not oxFlag
    End Sub
End Class

99-7彰商

image

Public Class Form1
    Dim a, b, j, i
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        a = Val(TextBox1.Text)
        b = Val(TextBox1.Text)
        j = 0
        For i = 1 To a Step 1
            j = j + i
        Next
        b = j
        TextBox2.Text = b
    End Sub
End Class

2009年3月4日 星期三

數字轉國字大寫 -- 精簡版

image

'數字轉國字大寫 -- 精簡版
Public Class Form1
    Dim i
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim s1 = "123450"
        Dim r1 = "拾佰仟萬拾佰仟億拾佰仟兆"
        Dim n = Len(s1)
        Dim s2 = Mid(s1, n, 1)

        Dim j = 1
        For i = n - 1 To 1 Step -1
            s2 = Mid(s1, i, 1) & Mid(r1, j, 1) & s2
            j = j + 1
        Next

        s2 = Replace(s2, "1", "壹")
        s2 = Replace(s2, "2", "貳")
        s2 = Replace(s2, "3", "參")
        s2 = Replace(s2, "4", "肆")
        s2 = Replace(s2, "5", "伍")
        s2 = Replace(s2, "6", "陸")
        s2 = Replace(s2, "7", "柒")
        s2 = Replace(s2, "8", "捌")
        s2 = Replace(s2, "9", "玖")
        s2 = Replace(s2, "0", "零")

        s2 = Replace(s2, "零拾", "零")
        s2 = Replace(s2, "零佰", "零")
        s2 = Replace(s2, "零仟", "零")

        s2 = Replace(s2, "零零零", "零")
        s2 = Replace(s2, "零零", "零")

        If Mid(s2, Len(s2), 1) = "零" Then s2 = Mid(s2, 1, Len(s2) - 1)
        If s2 = "" Then s2 = "零"

        MsgBox(s2)

    End Sub
End Class

2009年3月2日 星期一

麗山 50-2

image 

image

'2、寫一程式計算一組數字的乘積。你的程式必須再讀入0時停止,若讀入非0的
'    資料時,則予以計算乘積。
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text <> 0 Then
            If TextBox2.Text <> 0 Then
                TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text)
                Label3.Text = ""
            Else

                Label3.Text = "停止計算"
                TextBox3.Clear()
            End If
        ElseIf TextBox1.Text = 0 Then
            Label3.Text = "停止計算"
            TextBox3.Clear()
        End If

    End Sub
End Class