This page looks plain and unstyled because you're using a non-standard compliant browser. To see it in its best form, please visit upgrade to a browser that supports web standards. It's free and painless.

羅伯特學程式 會員登入 會員註冊

« 上一篇 | 下一篇 »

題目:
試 coding 一簡單的訂單程式, 可以輸入 客戶姓名, 地址及電話.
之後可輸入購買品名(可輸入一項以上)及數量.
之後, 再把剛剛 key in 的訂單狀況, 整個列印出來.

販售品名資料如下:

品名         單位     價格
----------------------
葡萄          斤       110
龍眼          斤       170
哈密瓜      斤       150
水蜜桃      斤       190


程式碼如後:

Module ConditionHomework_Robert
Sub main()
Dim name, address, tel As String
Dim item() As String = New String(4) {}
Dim price() As Integer = New Integer(4) {}
Dim unit() As Integer = New Integer(4) {}
Dim money() As Integer = New Integer(4) {}
Dim y As Integer
Dim ordInd As Integer = 0
Dim i As Integer
Dim sum As Integer = 0
'輸入客戶資料
Console.WriteLine("請輸入客戶資料.....")
Console.Write("客戶姓名: ")
name = Console.ReadLine()
Console.Write("客戶地址: ")
address = Console.ReadLine()
Console.Write("客戶電話: ")
tel = Console.ReadLine()
'輸入訂單明細
Do
ordInd += 1
Console.WriteLine()
Console.Write("請輸入品名或項次( 1->葡萄, 2->龍眼, 3->哈密瓜, 4->水蜜桃 ): ")
item(ordInd) = Console.ReadLine()
Console.Write("請輸入數量(斤): ")
unit(ordInd) = Console.ReadLine()
Select Case item(ordInd)
Case "葡萄"
Case "1"
money(ordInd) = unit(ordInd) * 110
item(ordInd) = "  葡萄"
Case "龍眼"
Case "2"
money(ordInd) = unit(ordInd) * 170
item(ordInd) = "  龍眼"
Case "哈密瓜"
Case "3"
money(ordInd) = unit(ordInd) * 150
item(ordInd) = "哈密瓜"
Case "水蜜桃"
Case "4"
money(ordInd) = unit(ordInd) * 190
item(ordInd) = "水蜜桃"
Case Else
Console.WriteLine("    *品名選擇有誤 !!")
ordInd = ordInd - 1
Continue Do
End Select
Console.Write("    是否繼續新增明細( 1->Yes,  0->No ): ")
y = Console.ReadLine()
sum = sum + money(ordInd)
Loop Until y = 0
'客戶資料, 訂單明細, 總金額
Console.WriteLine()
Console.WriteLine("--------------------  客戶資料及訂單明細  ----------------------------")
Console.WriteLine("客戶姓名:{0}", name)
Console.WriteLine("客戶地址:{0}", address)
Console.WriteLine("客戶電話:{0}", tel)
For i = 1 To ordInd
Console.WriteLine("第 {0} 項 --- 品名:{1}, 數量:{2}斤, 金額: 新台幣 {3}元", i, item(i), unit(i).ToString.PadLeft(10), money(i).ToString.PadLeft(10))
Next
Console.WriteLine("----------------------------------------------------------------------")
Console.WriteLine("總金額: 新台幣 {0:#,###}元", sum)
Console.WriteLine()
End Sub
End Module


執行後結果:
2009-09-19_02

**預計將此程式改為物件導向撰寫.

發表回應
  1. Re: [VB] 練習:極簡單訂單輸入及顯示(非物件導向) [回覆]
    Omega Watches 回應於 17 六月, 2010 14:42