2016年10月24日 星期一

單元:ch03. 決策指令


1. 視窗應用程式用到的輸出訊息盒 msgbox 語法是:

    MsgBox(提示訊息, [樣式, 視窗標題])

2. 視窗應用程式用到的輸入輸入盒 inputbox 語法是:

    InputBox(提示訊息, [標題文字, 預設值, 位置x, 位置y])

3. 可以用來加減乘除的叫數值,數值可分為整數和浮點數。如果你需要讓使用者可以輸入
整數,要寫出那 2 條指令?

Dim a As Integer
 a = InputBox()

4. 同上。如果你需要讓使用者可以輸入浮點數,要寫出那 2 條指令?

Dim a As Single
 a = InputBox()

5. 字串會用雙引號””框住,通常不會拿來+-*/,例如:姓名、電話、學號等。如果你需要讓
使用者可以輸入字串,要寫出那 2 條指令?

Dim a As String
 a = InputBox("")

6. 我會使用 If...then...。例如:判断整數 A 是正數則印出「正數」

Dim A As Single
If A > 0 Then
ans = "正數"
Else 
ans = "不是正數"

7. 我會使用 If...then...else。例如:判断整數 A 是正數則印出「正數」否則印出「負數」
作法 1:


作法 2:


8. 「成績等第判斷」使用巢狀 If...then...else...來完成。

 Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click        '成績等第判斷:操行成績0-100分成五等第()        Dim g As Integer        Dim ans As String = ""        g = InputBox("成績", 0)        If g <= 59 Then            ans = g & "丁"        Else            If g <= 69 Then                ans = g & "丙"            Else                If g <= 79 Then                    ans = g & "乙"                Else                    If g <= 89 Then                        ans = g & "甲"                    Else                        If g >= 90 Then                            ans = g & "優"                        End If                    End If                End If            End If        End If        Me.TextBox7.Text = ans    End Sub

9. 「成績等第判斷」使用 Select...case....來完成。


10. 輸入 A、B、C 三個數,輸出最大值。想出三種不同的結構來完成。


作法 1:


作法 2:


作法 3:

2016年9月29日 星期四


1.主控台應用程式用到的輸出方法有:

Console.WriteLine(“顯示文字”) &Console.Write(“顯示文字”)

2.主控台應用程式用到的輸入方法有:

變數=Console.ReadLine()&變數=Console.Read()

3.寫出 VB 運算子的優先順序(由上到下代表高到低)

算術運算 > 關係運算 > 邏輯運算

4.只用一條程式碼,印出上機練習題 1 的結果。(字串串接及程式碼換行排版練習):

Module Module1
    Sub Main()
        Console.WriteLine("====Console.WriteLine練習====") : Console.WriteLine("班級: 資應一甲") : Console.WriteLine("學號: 1110534040") : Console.WriteLine("班級: 姓名: 蔡維軒") : Console.WriteLine("=============================")
     
    End Sub
End Module

5.不恰當的資料型態宣告,會影響到結果的正確性。請參考 [ch02.資料的基本概念投影片] pp. 15-16 試寫一程式來印出各數值型別之表示範圍,並和課本比較是否有異。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim ans As String = "" '答案字串
        ans = "型態" & vbTab & "Byte" & vbTab & "範圍" & vbNewLine
        ans &= "SByte" & vbTab & "1" & vbTab & SByte.MinValue & "~" & SByte.MaxValue & vbNewLine
        ans &= "Byte" & vbTab & "1" & vbTab & Byte.MinValue & "~" & Byte.MaxValue & vbNewLine
        ans &= "Short" & vbTab & "2" & vbTab & Short.MinValue & "~" & Short.MaxValue & vbNewLine
        ans &= "UShort" & vbTab & "2" & vbTab & UShort.MinValue & "~" & UShort.MaxValue & vbNewLine
        ans &= "Integer" & vbTab & "4" & vbTab & Integer.MinValue & "~" & Integer.MaxValue & vbNewLine
        ans &= "UInteger" & vbTab & "4" & vbTab & UInteger.MinValue & "~" & UInteger.MaxValue & vbNewLine
        ans &= "Long" & vbTab & "8" & vbTab & Long.MinValue & "~" & Long.MaxValue & vbNewLine
        ans &= "ULong" & vbTab & "8" & vbTab & ULong.MinValue & "~" & ULong.MaxValue & vbNewLine
        ans &= "Single" & vbTab & "4" & vbTab & Single.MinValue & "~" & Double.MaxValue & vbNewLine
        ans &= "Double" & vbTab & "8" & vbTab & Double.MinValue & "~" & SByte.MaxValue & vbNewLine
        ans &= "Decimal" & vbTab & "12" & vbTab & Decimal.MinValue & "~" & Decimal.MaxValue & vbNewLine
        MsgBox(ans)
    End Sub

6.請各位寫一個文字模式的程式,導引使用者輸入數值 x,數值 y,展示此二數的各項算 術運算 ^ * / \ mod + - 結果(專案檔名: ch03-ConsoleIO-xxxxxxx ) 

Module Module1
    Sub Main()
        Console.WriteLine("輸入數值 x,數值 y,展示此二數的各項算術運算 ^ * / \ mod + - 結果")
        '輸入數值 x,數值 y
        Dim x, y As Single
        Console.Write("x=")
        x = Console.ReadLine
        Console.Write("y=")
        y = Console.ReadLine
        '展示此二數的各項算術運算 ^ * / \ mod + - 結果
        Console.WriteLine(x & "^" & y & "=" & x ^ y)
        Console.WriteLine(x & "*" & y & "=" & x * y)
        Console.WriteLine(x & "\" & y & "=" & x \ y)
        Console.WriteLine(x & "/" & y & "=" & x / y)
        Console.WriteLine(x & "mod" & y & "=" & x Mod y)
        Console.WriteLine(x & "+" & y & "=" & x + y)
        Console.WriteLine(x & "-" & y & "=" & x - y)
    End Sub
End Module    

7.輸入半徑 r , 印出此圓之「圓周長」及「圓面積」。 (專案檔名: ch03-ConsoleCircle-xxxxxxx-prj, 請用常數來定義圓周率 pi=3.14159265) 

  Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        '輸入半徑 r , 印出此圓之「圓周長」及「圓面積」。
        Dim ans As String = ""
        '輸入半徑 r
        Dim r As Single
        Const pi = 3.1415926
        r = InputBox("輸入半徑 r =", "輸入半徑 r , 印出此圓之「圓周長」及「圓面積」。")
        '印出此圓之「圓周長」
        ans &= ("圓周長=" & 2 * r * pi) & vbNewLine
        '及「圓面積」。
        ans &= ("圓面積=" & r ^ 2 * pi) & vbNewLine
        MsgBox(ans, , "結果")
    End Sub
End Class