在QBasic中创建菜单

时间:2013-12-29 06:10:05

标签: menu qbasic

我在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)dd数字
(S)ubtract数字
(M)最后的数字
(D)ivide数字
这一切都在一个以屏幕为中心的Box中。我正在使用LOCATE,但我没有得到我想要的结果,是的,我正在尝试没有图形。 我也使用INKEY$来获取用户输入的密钥。我试过了INPUT,但它回应了用户输入的字符。

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

2 个答案:

答案 0 :(得分: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应该仍然可靠。该页面上有索引的链接,或者您可以使用左侧导航区域中的搜索框来查找所需内容。

答案 1 :(得分:0)

我知道已有一个已接受的答案,但仍然以我自己的方式尝试。这是使用基本Q Basic语句的答案。据说我们将使用locate命令格式化输出并将菜单对齐到屏幕中心。

在开始之前,你必须知道,与其他编程语言不同,Q Basic只有25行和80列(都是从0编号)。 locate命令的语法: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 ...结束

    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

并确保填写所需的语句而非'添加所需的声明。 希望这会有所帮助...