1

我已经开始学习Qbasic。对于初学者练习,我从一个简单的文本游戏开始。一座山位于“北”,当您键入“北”时,控制台应在按 后打印“山” Enter。但是,当键入并Enter按下“north”时,代码不会被执行。这只是初学者的错误吗?我应该按不同的东西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
:
4

2 回答 2

4

在 DOSBox 中运行 QBasic 时,您的代码工作得很好,但显然 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 模拟器中)甚至像FreeBASICQB64这样的东西来运行你的程序,它们都基于 QBasic 并保留相同的语法,尽管我认为 QB64 可能是与原版兼容的多一点。

于 2016-04-28T02:04:35.097 回答
1

您还可以去掉尾随的 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$ 将只包含“北”..

于 2016-08-28T01:39:06.053 回答