输入在 Qbasic 中不起作用

Input not working in Qbasic

我已经开始学习 Qbasic。对于初学者练习,我从一个简单的文字游戏开始。一座山位于 "north",当您键入 "north" 时,控制台应在按 Enter 后打印 "Mountain"。但是,当键入 "north" 并按下 Enter 时,代码不会被执行。这只是初学者的错误吗?我应该按下不同于 Enter 的按钮吗?

代码如下:

CLS
PRINT "There is a mountain to the North"
PRINT "There is a cactus to the East"
PRINT "There is a river to the South"
PRINT "There is a shack to the East"
PRINT " "
INPUT "Type a direction:", direction$
IF direction$ = "north" THEN PRINT "Mountain"

以及 repl.it 的输出:

QBasic (qb.js)
Copyright (c) 2010 Steve Hanov
:
There is a mountain to the North
There is a cactus to the East
There is a river to the South
There is a shack to the East
:
Type a direction:  north
:

当 运行 QBasic 在 DOSBox 中时,您的代码工作得很好,但显然 repl.it 使用的 QB JavaScript 库不像 QBasic 那样工作。当您按 Enter 时,输入应该刚刚结束,并且不应存储行尾序列(或者应该自动删除)。不幸的是,JavaScript 库没有删除行尾序列。结果是以下在 QBasic 中不起作用时起作用:

IF direction$ = "north" + CHR$(10) THEN PRINT "Mountain"

事实上,我添加了一个简单的替代方法来测试解释器,但在我弄清楚 CHR$(10) 的问题之前收到了一个解析错误:

IF direction$ = "north" THEN PRINT "Mountain" ELSE PRINT "Not Mountain"

基于这个问题,我建议 运行 你的程序使用真实的东西(在像 DOSBox 这样的 DOS 模拟器中)或者甚至像 FreeBASIC or QB64 的东西,它们都基于 QBasic 并保留相同的语法,尽管我认为 QB64 可能与原始语法更兼容。

您还可以去除尾随的 ascii 字符:

INPUT X$
IF INSTR(X$, CHR$(10)) THEN
    X$ = LEFT$(X$, INSTR(X$, CHR$(10)) - 1) ' trim string
END IF
X$ = LCASE$(X$) ' and force case

这样 X$ 将只包含 'north'..