这是给你的一个老式的 IF 语句,但是有一个问题

发布于 2024-09-19 21:35:21 字数 968 浏览 3 评论 0原文

我在 QBASIC 中有一个 IF 语句...是的...QBASIC...

我一直在教某人编程(我认为这会很好并且很容易看到 语法如何工作)。

...无论如何,我有这样的代码:

CLS

start:
INPUT ">>", a$

PRINT a$
IF (INSTR(a$, "do you")) THEN
    IF (INSTR(a$, "like")) THEN
        IF (INSTR(a$, "cheese")) THEN PRINT "Yep, I like cheese":
        IF (INSTR(a$, "music")) THEN PRINT "Depends, which genre?": GOTO musicGenre
    ELSE IF (INSTR(a$, "hate")) THEN
            IF (INSTR(a$, "cheese")) THEN PRINT "No, I like cheese"
        END IF
    END IF
END IF


musicGenre:
INPUT ">>", m$
SELECT CASE (m$)
    CASE "pop"
        PRINT "..pop! lol, baa baa"
    CASE "rock"
        PRINT "Rock is ok"
END SELECT
GOTO start

但是当我输入“你喜欢奶酪吗?”时,它似乎只每隔一次回复“是的,我喜欢奶酪” ...

有人能解释一下吗?

注意:
你喜欢音乐吗?每次都有效...

注释 2:
输出截图:
替代文字

I have an IF statement in QBASIC... yes... QBASIC...

I have been teaching someone to program (I decided this would be nice and easy to see
how the syntax works).

...Anyway, I have this code:

CLS

start:
INPUT ">>", a$

PRINT a$
IF (INSTR(a$, "do you")) THEN
    IF (INSTR(a$, "like")) THEN
        IF (INSTR(a$, "cheese")) THEN PRINT "Yep, I like cheese":
        IF (INSTR(a$, "music")) THEN PRINT "Depends, which genre?": GOTO musicGenre
    ELSE IF (INSTR(a$, "hate")) THEN
            IF (INSTR(a$, "cheese")) THEN PRINT "No, I like cheese"
        END IF
    END IF
END IF


musicGenre:
INPUT ">>", m$
SELECT CASE (m$)
    CASE "pop"
        PRINT "..pop! lol, baa baa"
    CASE "rock"
        PRINT "Rock is ok"
END SELECT
GOTO start

But when I type "do you like cheese?" it seems to only reply "Yep, I like cheese" every other time...

Could anyone shed some light on this?

note:

"do you like music?" works every time...

note 2:

Screenshot of the output:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失退 2024-09-26 21:35:21

您提供的代码看起来是正确的。

请尝试以下操作之一:

  • 如果可能,请向我们发送更大的代码示例。我猜错误超出了您提供的代码范围。
  • 在第一个 IF 之前输出输入 (a$),以确认您的代码能够处理预期的输入。
  • 在大多数语言中,FALSE 为零,true 则为其他值。但是,您可能希望使用以下 IF (INSTR(a$) > 0) 更加明确。

编辑:您应该在任何奶酪结果上添加goto start。否则,它将转到 musicGenre 代码。

Your code you provided appears correct.

Try one of the following:

  • If possible, send us a larger code sample. I'm guessing the error is outside the code you provided.
  • Output the input (a$) before the first IF to confirm your code will be working with the expected input.
  • In most languages, FALSE is zero and true is anything else. However, you may want to be more explicit with the following IF (INSTR(a$) > 0).

EDIT: You should put a goto start on any cheese result. Otherwise, it's going to the musicGenre code.

2024-09-26 21:35:21
CLS

start:
    INPUT ">>", a$

    IF (INSTR(1, a$, "do you")) THEN
        IF (INSTR(1, a$, "like")) THEN
            IF (INSTR(1, a$, "cheese")) THEN PRINT "Yep, I like cheese"
            IF (INSTR(1, a$, "music")) THEN PRINT "Depends, which genre?": GOSUB musicGenre
        END IF
    IF (INSTR(1, a$, "hate")) THEN
        IF (INSTR(1, a$, "cheese")) THEN PRINT "No, I like cheese"
    END IF
END IF

GOTO start
musicGenre:
    INPUT ">>", m$
    SELECT CASE (m$)
        CASE "pop"
            PRINT "..pop! lol, baa baa"
        CASE "rock"
            PRINT "Rock is ok"
    END SELECT
RETURN
CLS

start:
    INPUT ">>", a$

    IF (INSTR(1, a$, "do you")) THEN
        IF (INSTR(1, a$, "like")) THEN
            IF (INSTR(1, a$, "cheese")) THEN PRINT "Yep, I like cheese"
            IF (INSTR(1, a$, "music")) THEN PRINT "Depends, which genre?": GOSUB musicGenre
        END IF
    IF (INSTR(1, a$, "hate")) THEN
        IF (INSTR(1, a$, "cheese")) THEN PRINT "No, I like cheese"
    END IF
END IF

GOTO start
musicGenre:
    INPUT ">>", m$
    SELECT CASE (m$)
        CASE "pop"
            PRINT "..pop! lol, baa baa"
        CASE "rock"
            PRINT "Rock is ok"
    END SELECT
RETURN
岁月无声 2024-09-26 21:35:21

该程序演示了在 Basic 中解析输入和 gosub。

REM Cheese progran source:
CLS
DO
    INPUT ">>", a$
    a$ = LCASE$(a$)
    PRINT a$
    IF INSTR(a$, "do you") THEN
        IF INSTR(a$, "like") THEN
            IF INSTR(a$, "cheese") THEN
                PRINT "Yep, I like cheese":
            END IF
            IF INSTR(a$, "music") THEN
                PRINT "Depends, which genre?"
                GOSUB MusicGenre
            END IF
        ELSE
            IF INSTR(a$, "hate") THEN
                IF INSTR(a$, "cheese") THEN
                    PRINT "No, I like cheese"
                END IF
            END IF
        END IF
    END IF
LOOP
END
MusicGenre:
INPUT ">>>", m$
a$ = LCASE$(a$)
SELECT CASE (m$)
    CASE "pop"
        PRINT "..pop! lol, baa baa"
    CASE "rock"
        PRINT "Rock is ok"
END SELECT
RETURN

This program demonstrates parsing input and gosubs in Basic.

REM Cheese progran source:
CLS
DO
    INPUT ">>", a$
    a$ = LCASE$(a$)
    PRINT a$
    IF INSTR(a$, "do you") THEN
        IF INSTR(a$, "like") THEN
            IF INSTR(a$, "cheese") THEN
                PRINT "Yep, I like cheese":
            END IF
            IF INSTR(a$, "music") THEN
                PRINT "Depends, which genre?"
                GOSUB MusicGenre
            END IF
        ELSE
            IF INSTR(a$, "hate") THEN
                IF INSTR(a$, "cheese") THEN
                    PRINT "No, I like cheese"
                END IF
            END IF
        END IF
    END IF
LOOP
END
MusicGenre:
INPUT ">>>", m$
a$ = LCASE$(a$)
SELECT CASE (m$)
    CASE "pop"
        PRINT "..pop! lol, baa baa"
    CASE "rock"
        PRINT "Rock is ok"
END SELECT
RETURN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文