我正在爲我的Visual Basic類創建一個項目,即創建數字檢查。該任務要求我們輸入一個支票金額,將其轉化爲單詞。例如,$ 1,200.00需要輸出「一千二百元」使用案例/方法將檢查金額轉換爲單詞
大多數情況下,我的代碼工作。我正在使用switch語句。最初的任務是讓我們的支票達到9,999的價值,但隨着我們繼續建立,我們需要能夠轉換爲99,999。
正如我所說的,我一直在使用一系列case語句,但意識到這是一個非常「硬代碼」的方式,並希望創建一個可以檢查這些類型的東西的方法,但是我仍然對Visual Basic不熟悉,並且真的不知道從哪裏開始或者在這種情況下適用什麼(我們沒有真正的示例)。
這是我的WriteCheck方法,大部分分配/轉換。
'Convert check value from a text field to a double' Try checkValue = checkInput.Text Catch ex As InvalidCastException MessageBox.Show("You must enter a numbers to write a check.") End Try 'Operation to convert number to String' thousands = checkValue \ 1000 hundreds = checkValue Mod 1000 hundreds = hundreds \ 100 tens = checkValue Mod 100 tens = tens \ 10 ones = checkValue Mod 10 ones = ones \ 1 'Case for thousands' Select Case thousands & hundreds & tens Case 1 tempStringT = "One" Case 2 tempStringT = "Two" Case 3 tempStringT = "Three" Case 4 tempStringT = "Four" Case 5 tempStringT = "Five" Case 6 tempStringT = "Six" Case 7 tempStringT = "Seven" Case 8 tempStringT = "Eight" Case 9 tempStringT = "Nine" End Select 'Case for hundreds' Select Case hundreds Case 1 tempStringH = "one" Case 2 tempStringH = "two" Case 3 tempStringH = "three" Case 4 tempStringH = "four" Case 5 tempStringH = "five" Case 6 tempStringH = "six" Case 7 tempStringH = "seven" Case 8 tempStringH = "eight" Case 9 tempStringH = "nine" End Select 'Case for tens' Select Case tens Or ones Case 1 tempStringTens = "one" Case 2 tempStringTens = "twenty" Case 3 tempStringTens = "thirty" Case 4 tempStringTens = "fourty" Case 5 tempStringTens = "fifty" Case 6 tempStringTens = "sixty" Case 7 tempStringTens = "seventy" Case 8 tempStringTens = "eighty" Case 9 tempStringTens = "ninety" End Select If tempStringTens <> "one" Then 'Case for ones' Select Case ones Case 1 tempStringO = "one" Case 2 tempStringO = "two" Case 3 tempStringO = "three" Case 4 tempStringO = "four" Case 5 tempStringO = "five" Case 6 tempStringO = "six" Case 7 tempStringO = "seven" Case 8 tempStringO = "eight" Case 9 tempStringO = "nine" End Select lblConverted.Text = tempStringT & " thousand " & tempStringH & " hundred " & tempStringTens & " " & tempStringO & " dollars " & change & "/100" End If If tempStringTens = "one" Then Select Case ones Case 1 tempStringO = "eleven" Case 2 tempStringO = "twelve" Case 3 tempStringO = "thirteen" Case 4 tempStringO = "fourteen" Case 5 tempStringO = "fifteen" Case 6 tempStringO = "sixteen" Case 7 tempStringO = "seventeen" Case 8 tempStringO = "eighteen" Case 9 tempStringO = "nineteen" End Select lblConverted.Text = tempStringT & " thousand " & tempStringH & " hundred " & tempStringO & " dollars" End If End Sub
這是「VB」還是「VBA」?很明顯,這個問題在一定程度上是通用的,但是您可能希望用最佳專業知識標記爲「VB」。 – Smandoli
謝謝,我實際上很難弄清楚如何標記這個,所以我只給了一個鏡頭! –
這個問題是不正確的,因爲您要求我們構建整個算法。你應該自己嘗試一下,然後在問這裏之前達到一個可以接受的階段。作爲一個快速提示,爲了減少這種大量的硬編碼,您必須找到常見的部分,只有易於自動化的位變化。例如,代替「Dim val1 As String =」1000「如果param = 2那麼val1 =」2000「ElseIf param = 3然後Val1 =」3000「,等等,你可以這樣做:'function calculateVal1(param As Integer )As String 返回param.ToString()&「000」 End Function'和'val1 = calculateVal1(param)'。 – varocarbas