1

我正在 QBasic 中制作一个简单的计算器,为了实现一个菜单,我遵循的做法是:

PRINT "Select an Option"
PRINT "1. Addition"
PRINT "2. Subtraction"
PRINT "3. Multiplycation"
PRINT "4. Division"
PRINT "Option No.: "
INPUT opt
CLS
SELECT CASE opt
        CASE 1
                PRINT "You have selected Addition"
                PRINT "Enter a no.:"
                INPUT n1
                PRINT "Enter second no.:"
                INPUT n2
                PRINT "The Sum is "; n1 + n2
        CASE 2
                PRINT "You have selected Subtraction"
                PRINT "Enter a no.:"
                INPUT n1
                PRINT "Enter second no.:"
                INPUT n2
                PRINT "Difference between "; n1; " and "; n2; " is "; n1 - n2
        CASE 3
                PRINT "You have selected Multiplycation"
                PRINT "Enter a no."
                INPUT n1
                PRINT "Enter second no.:"
                INPUT n2
                PRINT "Product is "; n1 * n2
        CASE 4
                PRINT "You have Selected Division"
                PRINT "Enter a no.:"
                INPUT n1
                PRINT "Enter second no.:"
                INPUT n2
                PRINT "The Quotient is "; n1 / n2; " and the remainder is "; n1 MOD n2
        CASE ELSE
                PRINT "Invalid Option Number"
END SELECT

但是这一次我想要一个更复杂的,比如:
(A)加数字
(S)减数字
(M)乘数字
(D)除数字
这一切都在一个以屏幕为中心的盒子里。我正在使用LOCATE,但我没有得到我想要的结果,是的,我正在尝试这个没有图形。我也INKEY$用来获取用户输入的密钥。我试过INPUT了,但它与用户输入的字符相呼应。

编辑:@user2864740,这意味着我无法获得完美的坐标。

4

2 回答 2

1

这是给你的东西。这是相当基本的(没有双关语),但值得一看。我会说它未经测试,因此它可能无法按预期工作。它应该给你一个关于如何做事的想法。

k$ = ""
CLS

' Print the menu and get the operation to perform.
WHILE k$ = ""

    LOCATE 8, 30
    PRINT "Select an option:"
    PRINT
    LOCATE , 30
    PRINT "(A)ddition"
    LOCATE , 30
    PRINT "(S)ubtraction"
    LOCATE , 30
    PRINT "(M)ultiplication"
    LOCATE , 30
    PRINT "(D)ivision"
    PRINT
    LOCATE , 30
    PRINT "Choice: ";

    ' Wait for a key press.
    WHILE k$ = ""
        k$ = INKEY$
    WEND

    SELECT CASE k$
    CASE "A", "S", "M", "D", "a", "s", "m", "d"
        ' Valid option entered.
    CASE ELSE
        k$ = ""
        LOCATE 17, 30
        PRINT "Invalid choice. Try again."
    END SELECT

WEND 'WHILE k$ = ""

' Making the choice uppercase means testing only "A"
'     works instead of testing for "A" and "a".
' The same is true with the other choices.
k$ = UCASE$(k$)

PRINT "You selected: ";
SELECT CASE k$
CASE "A": PRINT "Addition"
CASE "S": PRINT "Subtraction"
CASE "M": PRINT "Multiplication"
CASE "D": PRINT "Division"
END SELECT

另外,使用时要小心INKEY$。例如,按箭头键。箭头键是扩展键的一个例子,还有其他的。这就是为什么要按原样设计循环的原因。LEN(INKEY$) > 1当按下扩展键时,与字母或数字不同,其中LEN(INKEY$) = 1.

如果您需要参考资料,http: //www.qb64.net/wiki/ 上的 wiki应该仍然可靠。该页面上有一个指向索引的链接,或者您可以使用左侧导航区域中的搜索框来查找您需要的内容。

于 2013-12-29T18:17:32.087 回答
0

我知道已经有一个公认的答案,但仍然以我自己的方式尝试它。这是使用基本 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

并确保您填写了需要声明的地方,而不是像“加法所需的声明”之类的地方。希望这可以帮助...

于 2017-12-16T17:12:21.727 回答