第8~12章 實作題檢討

第8章

1. 簡單, 利用 for i = 1 to 5 來輸入a陣列, 再利用 for i = 5 to 1 step -1 來輸出a陣列

Dim a(5)
For i = 1 To 5
 a(i) = Val(InputBox("輸入數值"))
Next i
For i = 5 To 1 Step -1
 s = s & a(i) & " , "
Next i
MsgBox s

2. 如下:

c = 0
Sum = 0
Do
 a = Val(InputBox("輸入數值"))
 c = c + 1
 Sum = Sum + a
Loop Until a = 0
MsgBox "全部數值的平均為 " & Sum / (c - 1)

3. 如下:

Dim a(4)
a(1) = "春"
a(2) = "夏"
a(3) = "秋"
a(4) = "冬"

 b = Val(InputBox("請輸入1~4其中一個數字"))
 MsgBox b & "代表" & a(b) & "季"

4. 題目改成 3x4的二維陣列放在Excel中, 按下計算鈕會計算所有陣列元素的總和(提示: cells(i,j)就是二維陣列)

Sum = 0
For i = 1 To 3
 For j = 1 To 4
  Sum = Sum + Cells(i, j)
 Next j
Next i
Cells(5, 4) = Sum

5. 題目改成 歌唱大賽4位歌者進入總決賽, 有5位評審參與評分工作, 但每個歌者的得分, 是5位評審分數中去掉最高與最低分再總合平均, 4位歌者5位評審的分數在Excel中, 按下計算鈕會計算每位歌者的得分

Dim a(5)
For k = 2 To 5
 Sum = 0
 Max = 0
 Min = 99
 For j = 1 To 5
  a(j) = Cells(j + 1, k)
  Sum = Sum + a(j)
  If Max < a(j) Then
   Max = a(j)
  End If
  If Min > a(j) Then
   Min = a(j)
  End If
 Next j
 Cells(7, k) = (Sum - Max - Min) / 3
Next k

6.

第9章

1. 如下:

Private Sub CommandButton2_Click()
ans = 0
For i = 1 To 11 Step 2
 ans = ans + f(i)
Next i
MsgBox "答案是" & ans
End Sub

Function f(N)
Sum = 1
For i = 1 To N
 Sum = Sum * i
Next i
f = Sum
End Function

2. 如下:

Private Sub CommandButton3_Click()
x = Val(InputBox("請輸入x值"))
y = f(x)
MsgBox "Y是" & y
End Sub

Function f(x)
If x <= 9.8 Then
 f = 2 * x + 10
ElseIf x <= 19.6 Then
 f = x ^ 2 + 2 * x + 1
Else
 f = x ^ 4 + x ^ 2 - 20
End If
End Function

3. 如下:

Private Sub CommandButton2_Click()
n = Val(InputBox("請輸入N值"))
ans = sum(n)
MsgBox "累加的答案是" & ans
End Sub

Function sum(n)
If n = 1 Then
 sum = 1
Else
 sum = n + sum(n - 1)
End If
End Function

4. 利用文字方塊來輸入"牛肉麵50", 另一個文字方塊輸入"4碗", 按下按鈕會在標籤上顯示"牛肉麵4碗共200"。(假設:金額是2位數,數量是1位數)

a = TextBox1.Text
b = TextBox2.Text
k = Len(a)
price = Val(Right(a, 2))
Order = Left(a, k - 2)
amount = Val(Left(b, 1))
total = price * amount
Label1.Caption = Order & b & "共" & total

第10章

1. 簡單, 利用3個文字方塊供使用者輸入成績, 再用3個變數來儲存這3個成績(記得要用 val( )來轉換型態), 最後算出平均

2. 寫一個萬用累加程式, 可以算出 a到 b, 差值為 c 的答案

a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = Val(TextBox3.Text)
sum = 0
For i = a To b Step c
 sum = sum + i
Next i
Label2.Caption = "累加值為" & sum

 

第11章

1. 簡單

2. 如下:

If CheckBox1.Value = True Then
 Sum = 30 * Val(TextBox1.Text)
End If
If CheckBox2.Value = True Then
 Sum = Sum + 70 * Val(TextBox2.Text)
End If
If CheckBox3.Value = True Then
 Sum = Sum + 65 * Val(TextBox3.Text)
End If
If CheckBox4.Value = True Then
 Sum = Sum + 99 * Val(TextBox4.Text)
End If
If OptionButton2.Value = True Then
 Sum = Sum + 50
End If
Label1.Caption = "總共" & Sum & "元"

3. 設計一個心理測驗程式,測驗使用者的樂觀狀況(回答3個Yes,是樂觀的人。回答2個Yes,是快樂的人。回答1個Yes,是正常人。回答0個Yes,是有點悲觀。)


c = 0
If OptionButton1.Value = True Then
 c = c + 1
End If
If OptionButton3.Value = True Then
 c = c + 1
End If
If OptionButton5.Value = True Then
 c = c + 1
End If
Select Case c
 Case 3:
  Label4.Caption = "你是很樂觀的人"
 Case 2:
  Label4.Caption = "你是快樂的人"
 Case 1:
  Label4.Caption = "你是正常人"
 Case 0:
  Label4.Caption = "你有點悲觀"
End Select

第12章

1,2,3. 如下:

Private Sub CommandButton1_Click()
ListBox1.AddItem TextBox1.Text
End Sub

Private Sub CommandButton2_Click()
If ListBox1.ListIndex <> -1 Then
 ListBox2.AddItem ListBox1.Text
 ListBox1.RemoveItem ListBox1.ListIndex
End If
End Sub

Private Sub CommandButton3_Click()
If ListBox2.ListIndex <> -1 Then
 ListBox1.AddItem ListBox2.Text
 ListBox2.RemoveItem ListBox2.ListIndex
End If
End Sub

Private Sub CommandButton4_Click()
ListBox2.Clear
End Sub

4. 類似上題,把"資料回收區"改成"資料排序區", 按下"排序"鈕, 即會把"資料回收區"的資料排好放入"資料排序區"

Private Sub CommandButton1_Click()
ListBox1.AddItem TextBox1.Text
End Sub

Private Sub CommandButton2_Click()
N = ListBox1.ListCount
 For j = 1 To N
  Min = "zzzzzzz"
  For i = 0 To ListBox1.ListCount - 1
   If Min > ListBox1.List(i) Then
    Min = ListBox1.List(i)
    mini = i
   End If
  Next i

  ListBox2.AddItem Min
  ListBox1.RemoveItem mini
 Next j
End Sub