首页 > 解决方案 > JustBasic,在代码中出现“错误的嵌套错误选择/案例”

问题描述

对编程很陌生,如果是基本问题,抱歉,第一次做模块化工作。不知道我在这里做错了什么,但是第 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

标签: nestedsubroutinebasicselect-case

解决方案


Case指令属于Select Case指令。他们不能分开!您的程序必须始终尊重下面的布局而不干预Return指令

  Select Case <expression>
    Case <values>
      <action>
    Case <values>
      <action>
    Case <values>
      <action>
  End Select

没有子程序的解决方案

您将执行操作的指令直接插入相应Case指令的下方。缺点是,如果动作块中的指令数量增加,您将失去对整个Select Case构造的跟踪。

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

使用子程序DepositWithdrawalShowBalance的解决方案

您将执行操作的指令放在单独的子程序中,并在相应指令的Gosub正下方插入一个。Case优点是,即使操作块中的指令数量增加,您也可以清楚地了解整个Select Case结构。

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

推荐阅读