我正在尝试缩进一个伪代码,我有 A 列并且没有缩进。基本上我试图在什么时候添加 4 个空格,即使有嵌套的 if/elseif/else/end 块或 while/end 块
这是我的输入:
WHILE TRUE
DISPLAY "Welcome to Bank ATM - Cash Withdrawal"
amount = INPUT "How much would you like to withdraw today?"
IF (amount MOD 10) != 0 THEN
DISPLAY "You can only withdraw a multiple of ten!"
ELSE
IF amount<10 THEN
DISPLAY "You can withdraw only above of £10"
ELSEIF amount>1000 THEN
DISPLAY "You can only withdraw between £10 and £1000"
ELSE
notes20 = amount DIV 20
notes10 = (amount MOD 20) / 10
DISPLAY "Collect your money: "
DISPLAY " >> £20 Banknotes: " + notes20
DISPLAY " >> £10 Banknotes: " + notes10
DISPLAY "Thank you for using this Python Bank ATM."
DISPLAY "Good Bye."
END
END
END
这是我当前的尝试,当前打印的内容与输入完全相同。
Sub indent()
a = WorksheetFunction.Transpose(Range("A1:A21"))
if_count = 0
end_count = 0
For i = 1 To UBound(a)
If a(i) = "IF" Then
If (if_count = 0 And end_count = 0) Then
Debug.Print a(i)
if_count = if_count + 1
ElseIf if_count > 0 Then
Debug.Print " " & a(i)
if_count = 0
Else
Debug.Print a(i)
End If
Else
Debug.Print a(i)
End If
Next
End Sub
我正在寻找的输出是这样的:
WHILE TRUE
DISPLAY "Welcome to Python Bank ATM - Cash Withdrawal"
amount = INPUT "How much would you like to withdraw today?"
IF (amount MOD 10) != 0 THEN
DISPLAY "You can only withdraw a multiple of ten!"
ELSE
IF amount<10 THEN
DISPLAY "You can withdraw only above of £10"
ELSEIF amount>1000 THEN
DISPLAY "You can only withdraw between £10 and £1000"
ELSE
notes20 = amount DIV 20
notes10 = (amount MOD 20) / 10
DISPLAY "Collect your money: "
DISPLAY " >> £20 Banknotes: " + notes20
DISPLAY " >> £10 Banknotes: " + notes10
DISPLAY "Thank you for using this Python Bank ATM."
DISPLAY "Good Bye."
END
END
END
感谢您的帮助。
Case
子句)Option Explicit
Sub Indent()
Dim i As Long, arr, sKey As String, iCnt As Long, sPad As String
Const S_CHAR = " " ' four space
arr = WorksheetFunction.Transpose(Range("A1:A21"))
For i = 1 To UBound(arr)
Select Case UCase(Split(arr(i))(0))
Case "WHILE", "IF"
sPad = Application.Rept(S_CHAR, iCnt)
iCnt = iCnt + 1
Case "ELSE", "ELSEIF"
sPad = Application.Rept(S_CHAR, iCnt - 1)
Case "END"
iCnt = iCnt - 1
sPad = Application.Rept(S_CHAR, iCnt)
Case Else
sPad = Application.Rept(S_CHAR, iCnt)
End Select
Debug.Print sPad & arr(i)
Next
End Sub