我知道已经有一个公认的答案,但仍然以我自己的方式尝试它。这是使用基本 Q Basic 语句的答案。如前所述,我们将使用locate
命令来格式化输出并将菜单与屏幕中心对齐。
在开始之前,您必须知道,与其他编程语言不同,Q Basic 只有 25 行和 80 列(均从 0 开始编号)。
定位命令的语法:LOCATE ROW, COLUMN : STATEMENT
这是在屏幕中间打印字符串的方法:首先计算字符串的长度,字符串的一半必须打印在右侧,其余部分打印在中间列的左侧(中间列是 40)。因此列号将是:40 -(字符串长度/2)。如果列号相差一个或两个数字,这不是问题。
类型 1:
如果您希望菜单居中并且单词按以下顺序排列(所有首字母对齐)
(A)dd Numbers
(S)ubtract Numbers
(M)ultiply Numbers
(D)ivide Numbers
在这里,我们将使用字符串计算列号,(M)ultiply Numbers
或者(S)ubtract Numbers
因为它们中的字符数最多。因此列号为 40 - (18 / 2) = 31。
然后使用 If..Elseif..Else 的命令是...
LOCATE 2, 31 : PRINT "(A)dd Numbers"
LOCATE 3, 31 : PRINT "(S)ubtract Numbers"
LOCATE 4, 31 : PRINT "(M)ultiply Numbers"
LOCATE 5, 31 : PRINT "(D)ivide Numbers"
LOCATE 6, 31 : INPUT "ENTER OPTION" ; OPT$
INPUT BLNK 'user must press enter
CLS
'You don't need to worry about whether the string is in upper case or lower
'case as we are going to compare the value only of the string converted
'into upper case using ucase$ function
IF UCASE$(OPT$) = "A" THEN
'The statements required for addition
ELSEIF UCASE$(OPT$) = "S" THEN
'The statements required for subtraction
ELSEIF UCASE$(OPT$) = "M" THEN
'The statements required for multiplication
ELSEIF UCASE$(OPT$) = "D" THEN
'The statements required for division
ELSE PRINT "INVALID OPTION"
END IF
END
同一个程序用例
LOCATE 2, 31 : PRINT "(A)dd Numbers"
LOCATE 3, 31 : PRINT "(S)ubtract Numbers"
LOCATE 4, 31 : PRINT "(M)ultiply Numbers"
LOCATE 5, 31 : PRINT "(D)ivide Numbers"
LOCATE 6, 31 : INPUT "ENTER OPTION" ; OPT$
INPUT BLNK 'user must press enter
CLS
OP$ = UCASE$(OPT$)
SELECT OP$
CASE "A"
'The statements required for addition
CASE "B"
'The statements required for subtraction
CASE "M"
'The statements required for multiplication
CASE "D"
'The statements required for division
CASE ELSE
PRINT "INVALID OPTION"
END SELECT
END
类型 2:
如果您希望菜单以所有选项为中心,则如下所示:-
(A)dd Numbers
(S)ubtract Numbers
(M)ultiply Numbers
(D)ivide Numbers
然后代码是......(但我还是更喜欢使用前一个,它看起来好多了)使用 If..elseif...end
LOCATE 2, 34 : PRINT "(A)dd Numbers"
LOCATE 3, 31 : PRINT "(S)ubtract Numbers"
LOCATE 4, 31 : PRINT "(M)ultiply Numbers"
LOCATE 5, 32 : PRINT "(D)ivide Numbers"
LOCATE 6, 33 : INPUT "ENTER OPTION"; OPT$
INPUT BLNK 'user must press enter
CLS
IF UCASE$(OPT$) = "A" THEN
'The statements required for addition
ELSEIF UCASE$(OPT$) = "S" THEN
'The statements required for subtraction
ELSEIF UCASE$(OPT$) = "M" THEN
'The statements required for multiplication
ELSEIF UCASE$(OPT$) = "D" THEN
'The statements required for division
ELSE PRINT "INVALID OPTION"
END IF
END
使用 Case 的相同程序...
LOCATE 2, 34 : PRINT "(A)dd Numbers"
LOCATE 3, 31 : PRINT "(S)ubtract Numbers"
LOCATE 4, 31 : PRINT "(M)ultiply Numbers"
LOCATE 5, 32 : PRINT "(D)ivide Numbers"
LOCATE 6, 33 : INPUT "ENTER OPTION"; OPT$
INPUT BLNK 'user must press enter
CLS
OP$ = UCASE$(OPT$)
SELECT OP$
CASE "A"
'The statements required for addition
CASE "B"
'The statements required for subtraction
CASE "M"
'The statements required for multiplication
CASE "D"
'The statements required for division
CASE ELSE
PRINT "INVALID OPTION"
END SELECT
END
并确保您填写了需要声明的地方,而不是像“加法所需的声明”之类的地方。希望这可以帮助...