JustBasic,在代碼中出現“錯誤的嵌套錯誤選擇/案例” (JustBasic, getting "bad nesting error select/case" in code)


問題描述

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 TSep Roland)

參考文件

  1. JustBasic, getting "bad nesting error select/case" in code (CC BY‑SA 2.5/3.0/4.0)

#select-case #subroutine #nested #Basic






相關問題

SQL 加入 2 個共享列的查詢 (SQL joining 2 queries that share a column)

sql中的條件總和,根據字段中的條件從另一個表中獲取正確的總和 (conditional sum in sql, get correct sum from another table based on a condition in a field)

如何對此查詢進行選擇案例? (How can I do select case to this query?)

聲明一個案例列表在多個地方使用 (Declaring a case list to be used in more than one place)

選擇案例語句中的數組是否可能? (Is an array inside a select case statement possible?)

將 VBA 選擇案例轉換為 C# (Convert VBA select case to C#)

使用 Word VBA 中的宏選擇大小寫將英寸轉換為毫米 (Convert inch to mm using macro select Case in Word VBA)

訪問,選擇案例 ActiveControl.Name? (Access, Select Case ActiveControl.Name?)

選擇不運行每個案例的案例 (Select Case Not Running Every Case)

JustBasic,在代碼中出現“錯誤的嵌套錯誤選擇/案例” (JustBasic, getting "bad nesting error select/case" in code)







留言討論