snowmhiau 常駐此論壇的衛士
註冊時間: 2007-06-09 文章: 116 來自: KEELONG 692.99 果凍幣
|
發表於: 2008-7-29, PM 10:59 星期二 文章主題: [VB]今天上VB的函數,還有程序(研討) |
|
|
例題1
這段是老師示範
代碼: | Module Module1
Sub Main()
Dim x As Date
Dim iYear As Short
Dim iMonth, iDay, iWeekday As Byte
Console.Write("請輸入一日期:")
x = Console.ReadLine
iYear = Year(x)
Console.WriteLine()
Console.Write("這一天是在" + Str(iYear) + "年")
Console.WriteLine()
iMonth = Month(x)
Console.Write("這一天是在" + Str(iMonth) + "月")
Console.WriteLine()
iDay = Day(x)
Console.Write("這一天是在" + Str(iDay) + "日")
Console.WriteLine()
iWeekday = Weekday(x)
Select Case iWeekday
Case 1
Console.Write("這一天是星期日")
Case 2
Console.Write("這一天是星期一")
Case 3
Console.Write("這一天是星期二")
Case 4
Console.Write("這一天是星期三")
Case 5
Console.Write("這一天是星期四")
Case 6
Console.Write("這一天是星期五")
Case 7
Console.Write("這一天是星期六")
End Select
Console.ReadLine()
End Sub
End Module |
下面是我自己想的
代碼: | Module Module1
Sub Main()
Dim a As Date
Console.WriteLine("請輸入任一日期 (年/月/日) :")
a = Console.ReadLine()
Console.WriteLine(Year(a) & "年")
Console.WriteLine(Month(a) & "月")
Console.WriteLine(Day(a) & "日")
Select Case Weekday(a)
Case 1
Console.WriteLine("星期天")
Case 2
Console.WriteLine("星期一")
Case 3
Console.WriteLine("星期二")
Case 4
Console.WriteLine("星期三")
Case 5
Console.WriteLine("星期四")
Case 6
Console.WriteLine("星期五")
Case 7
Console.WriteLine("星期六")
End Select
Console.Read()
End Sub |
例題2
加入程序處理
老師的範例
代碼: | Module Module1
Sub Main()
Dim x As Date
Dim y As String = "Y"
Do
Console.WriteLine()
Console.Write("請輸入一日期:")
x = Console.ReadLine
Call selectYear(x)
Call selectMonth(x)
Call selectDay(x)
Call selectWeekDay(x)
Console.Write("是否繼續Y/N:")
y = Console.ReadLine
If UCase(y) <> "Y" Then
Exit Do
End If
Loop While UCase(y) = "Y"
Console.ReadLine()
End Sub
Private Sub selectYear(ByVal x As Date)
Dim iYear As Short
iYear = Year(x)
Console.Write("這一天是在" + Str(iYear) + "年")
Console.WriteLine()
End Sub
Private Sub selectMonth(ByVal x As Date)
Dim iMonth As Byte
iMonth = Month(x)
Console.Write("這一天是在" + Str(iMonth) + "月")
Console.WriteLine()
End Sub
Private Sub selectDay(ByVal x As Date)
Dim iDay As Byte
iDay = Day(x)
Console.Write("這一天是在" + Str(iDay) + "日")
Console.WriteLine()
End Sub
Private Sub selectWeekDay(ByVal x As Date)
Dim a As Byte
a = Weekday(x)
Select Case a
Case 1
Console.Write("這一天是星期日")
Case 2
Console.Write("這一天是星期一")
Case 3
Console.Write("這一天是星期二")
Case 4
Console.Write("這一天是星期三")
Case 5
Console.Write("這一天是星期四")
Case 6
Console.Write("這一天是星期五")
Case 7
Console.Write("這一天是星期六")
End Select
Console.WriteLine()
End Sub
End Module |
下面是自己想的
(我少加了 迴圈...@@"不知道我寫成這樣加上迴圈會有大問題嗎?)
代碼: | Module Module1
Sub Main()
Dim a As Date
Console.WriteLine("請輸入任一日期 (年/月/日) :")
a = Console.ReadLine()
Call an(a)
Call bn(a)
Call cn(a)
Call dn(a)
Console.Read()
End Sub
Private Sub an(ByVal a As Date)
Console.WriteLine(Year(a) & "年")
End Sub
Private Sub bn(ByVal a As Date)
Console.WriteLine(Month(a) & "月")
End Sub
Private Sub cn(ByVal a As Date)
Console.WriteLine(Day(a) & "日")
End Sub
Private Sub dn(ByVal a As Date)
Select Case Weekday(a)
Case 1
Console.WriteLine("星期天")
Case 2
Console.WriteLine("星期一")
Case 3
Console.WriteLine("星期二")
Case 4
Console.WriteLine("星期三")
Case 5
Console.WriteLine("星期四")
Case 6
Console.WriteLine("星期五")
Case 7
Console.WriteLine("星期六")
End Select
End Sub
End Module |
我執行兩個結果是一樣....但是不知道 在程序上會不會有什麼錯誤...或是邏輯上可以幫我修正一下....(希望能做得更好_請高手指點一下吧><")
還是說有老師沒說到的可以順便幫我補充嗎...? |
|