2016年10月13日 星期四

ch03. 決策指令





單元:ch03. 決策指令 Decision instruction

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

Answer1:  MsgBox()           (函數可以顯示訊息。)

 例如:確認操作,函數的語法如下所示:MsgBox(提示訊息, [樣式, 視窗標題])



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

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




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

Answer3 , 4 :  
        
               '宣告
                  Dim a as Integer (整數)
                  Dim a as Single or Double (單,雙精度浮點數) 第4題

               '用 Inputbox 輸入數值
           a = Inputbox(   ) '---------> Integer 的部分
                a = Inputbox(   ) '---------> Single,Double 的部分 第4題
if judge the condition 
   A / 1 = A 除 1 
   A \ 1 = A 除 1 的商數 
Example : 
           15 / 1 = 15
           15 \ 1 = 15
then ans = A & "是整數" (應該顯示的是 : 15是整數)

           15 .12415 / 1 = 15.12415
           15.12415  \ 1 = 15.12415
then ans = A & "是浮點數" (應該顯示的是 : 15.12415是浮點數)


5. 字串會用雙引號””框住,通常不會拿來+-*/,例如:姓名、電話、學號等。如果你需要讓使用者可以輸入字串,要寫出那 2 條指令?
   Answer 5 :
      Dim x  as String 
       x = Inputbox("","",0) 'Write a 0 My custom



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

     Answer 6 , 7 : 
        Dim A as Integer
        Dim ans as String = ""
       A = Inputbox("A =","我會使用 If...then...。例如:判断整數 A 是正數則印出正數",0)

      If A > 0 Then  '第7題的做法二就是反著寫,如圖所示
          ans = A & " is a positive number"   '第6題只有到這裡便可以輸出答案了...
      Else
          ans = A & " is a negative number"
     End If
     '(Msgbox,Textbox都可以...)
     '之後傳送位置依情況設置


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

     Answer 8 :
            Dim Score As Integer
            Dim Rank As String  = 0 ' 
        Score = InputBox("Score = ", "Arrangement the Rank of the Score", 0)
        If Score < 60 Then
            Rank = " Rank = " & " E"
        ElseIf Score < 70 Then
            Rank = " Rank = " & " D"
        ElseIf Score < 80 Then
            Rank = " Rank = " & " C"
        ElseIf Score < 90 Then
            Rank = " Rank = " & " B"
        Else
            Rank = " Rank = " & " A"
        End If
        'Rank = If(Score < 60, "Rank = E", If(Score < 70, "Rank = D", If(Score < 80, "Rank = C", If(Score < 90, "Rank = B", If(Score <= 100, "Rank = A", "")))))'三維函數,但值較適合小型運算,鮮有Boolean值

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


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

作法 1:


作法 2:
I'm using if a > c

作法 3:
I'm using if c > b





     單元:ch01. 第一程式、ch02. 資料基本概念
       
              1.主控台應用程式用到的輸出方法有: 
                 
                  1: Console.WriteLine   Meaning:("顯示文字")   不換行輸出

                  2: Console.Write           Meaning:("顯示文字")   換行輸出

             "   Console.WriteLine顯示後會自動換行,而Console.Write不會。     "               

Example: On September Twenty Third (Our First Program)



               
              2. 主控台應用程式用到的輸入方法有 :
                 
                   1:  變數= Console.ReadLine()             換行輸入字串

                   2:  變數= Console.Read()                    不換行輸入字元
 
               "The Difference:

     p.s   Console.ReadLine
            是等待從鍵盤輸入一串文字(字串),輸入的字串會存入指定的變數,輸入後須按Enter。

         而Console.Read        
             是等待從鍵盤輸入一個字元,不須按Enter,常被用在按任意鍵繼續。

                3.寫出 VB 運算子的優先順序(由上到下代表高到低)
                   
                       {算數運算子}            {邏輯運算子}          (排列順序從上至下)
                        
                         ^,                                  Not,     
   
                          -負號,                          And AndAlso,
         
                         */(乘除),                       Or OrElse,

                           \,                                  Xor
           
                       Mod,

                          +-

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


        Console.Write("====Console.WriteLine練習====" & vbCrLf & "班級:資應一甲" & vbCrLf & "學號:1110534011" & vbCrLf & "姓名:許端容" & vbCrLf & "=============================")

                   p.s.  vbCrLf 即 vbCr+vbLf = vbNewLine

Example : It has the same Effect too!!!!




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


Dim ans As String = "  (先做宣告)

    ans =  "型態" & vbTab & "Byte" & vbTab & "範圍" & vbNewLine
    ans = ans & "SByte" & vbTab & "1" & vbTab & SByte.MinValue & " ~ " & SByte.MaxValue & vbNewLine

    ans = ans & "Byte" & vbTab & "1" & vbTab & Byte.MinValue & " ~ " & Byte.MaxValue & vbNewLine

    ans = ans & "Short" & vbTab & "2" & vbTab & Short.MinValue & " ~ " & Short.MaxValue & vbNewLine

    ans = ans & "UShort" & vbTab & "2" & vbTab & UShort.MinValue & " ~ " & UShort.MaxValue & vbNewLine

    ans = ans & "Integer" & vbTab & "4" & vbTab & Integer.MinValue & " ~ " & Integer.MaxValue & vbNewLine

    
ans = ans & "UInteger" & vbTab & "4" & vbTab & UInteger.MinValue & " ~ " & UInteger.MaxValue & vbNewLine

    
ans = ans & "Long" & vbTab & "8" & vbTab & Long.MinValue & " ~ " & Long.MaxValue & vbNewLine

    
ans = ans & "ULong" & vbTab & "8" & vbTab & ULong.MinValue & " ~ " & ULong.MaxValue & vbNewLine

    ans = ans & "Single" & vbTab & "4" & vbTab & Single.MinValue & " ~ " & Single.MaxValue & vbNewLine

    ans = ans & "Double" & vbTab & "8" & vbTab & Double.MinValue & " ~ " & Double.MaxValue & vbNewLine

    ans = ans & "Decimal" & vbTab & "12" & vbTab & Decimal.MinValue & " ~ " & Decimal.MaxValue & vbNewLine
                     
                          (答案中的各種答案,並互相串聯,顯示各自極值)

                                                                                            跟課本是一樣的數值喔!    



              6.請各位寫一個文字模式的程式,導引使用者輸入數值 x,數值 y,展示此二數的各項算術運算 ^ * / \ mod + - 結果
    
 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






         7. 輸入半徑 r , 印出此圓之「圓周長」及「圓面積」。

      (專案檔名: ch03-ConsoleCircle-xxxxxxx-prj, 請用常數來定義圓周率 pi=3.14159265)

       '輸入半徑 r , 印出此圓之「圓周長」及「圓面積」。 
        Dim ans As String
        Const pi = 3.14159265                p.s. Meaning:Const為常數定義,與dim差不多,但const定义的不能修改a的值,dim 定义的可以重新赋值
       '輸入半徑 r 
        Dim r As Double
        r = InputBox("r=", "輸入半徑 r , 印出此圓之「圓周長」及「圓面積」。", 0)

       '印出此圓之「圓周長」及「圓面積」。
        ans &= "圓周長=" & 2 * pi * r & vbNewLine
        ans &= "圓面積=" & r ^ 2 * pi & vbNewLine

        MsgBox(ans)