問題描述
JustBasic,在代碼中出現“錯誤的嵌套錯誤選擇/案例” (JustBasic, getting "bad nesting error select/case" in code)
對編程很陌生,如果有基本問題,抱歉,第一次做模塊化工作。不知道我在這裡做錯了什麼,但第 41 行([ShowBalance] ‑‑> Case"C" 當我嘗試運行我的程序時在標題中吐出錯誤。什麼嵌套不正確?
Balance=1000
Print "Banking Menu"
Do
Gosub[Menu]
Gosub[Deposit]
Gosub[Withdrawal]
Gosub[ShowBalance]
Loop Until Choice$="Q"
Print "Thanks for using the Banking Application"
End
[Menu]
Print "A. Deposit"
Print "B. Withdrawal"
Print "C. Show Balance"
Print "Q. Quit"
Input "Make Selection: ";Choice$
Select Case Upper$(Choice$)
Return
[Deposit]
Case "A"
Input "Enter Amount to Deposit: ";Dep
If Dep > 0 Then
Balance=Balance+Dep
Else
Print "Invalid Amount"
End If
Return
[Withdrawal]
Case "B"
Input "Enter Amount to Withdraw: ";Wdrw
If Wdrw <= 1000 Then
Balance=Balance‑Wdrw
Else
Print "Invalid Amount"
Return
[ShowBalance]
Case "C"
Print
Print "*"
Print " Account Balance: $";using("####.##",Balance);" "
Print "*"
End Select
Print
Print "‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑"
Return
</code></pre>
參考解法
方法 1:
The Case
instructions belong to the Select Case
instruction. They must not get separated! Your program must always respect below layout without intervening Return
instructions.
Select Case <expression>
Case <values>
<action>
Case <values>
<action>
Case <values>
<action>
End Select
Solution without subroutines
You insert the instructions that perform the action directly below the corresponding Case
instruction. Disadvantage is that, if the number of instructions in an action block is elevated, you loose track of the whole Select Case
construct.
Balance=1000
Print "Banking Menu"
Do
Gosub[Menu]
Loop Until Choice$="Q"
Print "Thanks for using the Banking Application"
End
[Menu]
Print "A. Deposit"
Print "B. Withdrawal"
Print "C. Show Balance"
Print "Q. Quit"
Input "Make Selection: ";Choice$
Select Case Upper$(Choice$)
Case "A"
Input "Enter Amount to Deposit: ";Dep
If Dep > 0 Then
Balance=Balance+Dep
Else
Print "Invalid Amount"
End If
Case "B"
Input "Enter Amount to Withdraw: ";Wdrw
If Wdrw <= 1000 Then
Balance=Balance‑Wdrw
Else
Print "Invalid Amount"
End If
Case "C"
Print
Print "*********************************"
Print "*** Account Balance: $";using("####.##",Balance);" ***"
Print "*********************************"
Print
Print "‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑"
End Select
Return
Solution with subroutines Deposit, Withdrawal, and ShowBalance
You place the instructions that perform the action in separate subroutines and insert a mere Gosub
directly below the corresponding Case
instruction. Advantage is that, even if the number of instructions in an action block is elevated, you keep a clear view on the whole Select Case
construct.
Balance=1000
Print "Banking Menu"
Do
Gosub[Menu]
Loop Until Choice$="Q"
Print "Thanks for using the Banking Application"
End
[Menu]
Print "A. Deposit"
Print "B. Withdrawal"
Print "C. Show Balance"
Print "Q. Quit"
Input "Make Selection: ";Choice$
Select Case Upper$(Choice$)
Case "A"
Gosub[Deposit]
Case "B"
Gosub[Withdrawal]
Case "C"
Gosub[ShowBalance]
End Select
Return
[Deposit]
Input "Enter Amount to Deposit: ";Dep
If Dep > 0 Then
Balance=Balance+Dep
Else
Print "Invalid Amount"
End If
Return
[Withdrawal]
Input "Enter Amount to Withdraw: ";Wdrw
If Wdrw <= 1000 Then
Balance=Balance‑Wdrw
Else
Print "Invalid Amount"
End If
Return
[ShowBalance]
Print
Print "*********************************"
Print "*** Account Balance: $";using("####.##",Balance);" ***"
Print "*********************************"
Print
Print "‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑"
Return
(by David T、Sep Roland)
參考文件