首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >赋值5-3伪码检查

赋值5-3伪码检查
EN

Stack Overflow用户
提问于 2022-06-05 14:37:59
回答 1查看 273关注 0票数 -1

我已经完成了这个任务,但我只想有人检查一下,以确保它是正确的。以下说明:

创建伪代码或流程图,逻辑上勾勒出允许玩家在房间之间移动的步骤,使用命令向北、南、东和西移动。确保解决以下问题:

  • ,你需要从玩家那里输入什么?您将如何提示播放机输入?您将如何验证输入?

  • ,如果玩家进入一个有效的方向,程序应该做什么?输出结果是什么?

  • ,如果玩家输入无效的方向,程序应该做什么?将产生什么输出?
  • ,您将如何通过决策分支和循环控制程序流?

这是我的密码:

代码语言:javascript
运行
复制
START 
currentRm=’Foyer’
PRINT ‘You are in the Foyer
BEGIN LOOP
    INPUT ‘Which direction do you want to go?’
    IF currentRm IS ‘Foyer’
        IF direction IS ‘East’
                currentRm = ‘Kitchen’ 
                OUTPUT ‘You are in the Kitchen’
        ELSE
                OUTPUT ‘Invalid Direction.  Try again’

    ELIF currentRm IS ‘Kitchen’
    INPUT ‘Where do you want to go?’
        IF direction IS ‘North’
                            currentRm = ‘Living Room’
                OUTPUT’You are in the Living Room’
        ELIF direction IS ‘South’
                currentRm = ‘Dining Room’
                OUTPUT’You are in the Dining Room’
        ELIF direction IS ‘East’
                        currentRm = ‘Bedroom’
                OUTPUT ‘You are in the Bedroom’
        ELIF direction IS ‘West’
                            currentRm = ‘Foyer’
                OUTPUT ‘You are in the Foyer’
        ELSE
                OUTPUT  ‘Invalid Direction.  Try again’
    ELIF currentRM IS ‘Living Room’
    INPUT ‘Where do you want to go?’
        IF direction IS ‘East’
                currentRm = ‘Gaming Room’
                PRINT ‘You are in the Gaming Room’
        ELIF direction IS ‘South’   
                currentRm = ‘Kitchen’
                OUTPUT ‘You are in the Kitchen’
        ELSE
                OUTPUT ‘Invalid Direction.  Try again’

    ELIF currentRm IS ‘Gaming Room’
    INPUT ‘Where do you want to go?’
        IF direction IS ‘West’
                currentRm = ‘Living Room’
                OUTPUT ‘You are in the Living Room’
        ELSE
                OUTPUT ‘Invalid Direction.  Try again’

        ELIF currentRm IS ‘Dining Room’
        INPUT ‘Where do you want to go?’
        IF direction IS ‘North’
                currentRm = ‘Kitchen’
                OUTPUT ’You are in the Kitchen’
        ELIF direction IS ‘East’
                currentRm = ‘Garage’
                OUTPUT ‘You are in the Garage’
        ELSE
                OUTPUT ‘Invalid Direction.  Try again’

    ELIF currentRm IS ‘Garage’
    INPUT ‘Where do you want to go?’
        IF direction IS ‘West’
                 currentRm = ‘Dining Room’
                OUTPUT ‘You are in the Dining Room’
        ELSE
                OUTPUT ‘Invalid Direction.  Try again’

    ELIF currentRm IS ‘Bedroom’
    INPUT ‘Where do you want to go?’
                IF direction IS ‘West’
                 currentRm = ‘Kitchen’
                OUTPUT ‘You are in the Kitchen’
        ELIF direction IS ‘North’
                currentRm = ‘Bathroom’
                OUTPUT ‘You are in the Bathroom’
                ELSE
                   OUTPUT ‘Invalid Direction.  Try again’
    ELIF currentRm IS ‘Bathroom’
    INPUT ‘Where do you want to go?’
        IF direction is ‘South’
                currentRm = ‘Bedroom’
                OUTPUT ‘You are in the Bedroom’
        ELSE
                OUTPUT ‘Invalid Direction.  Try again’
    
END LOOP
        IF currentRm = ‘Gaming Room’
        OUTPUT ‘Game Over’
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-05 15:01:54

几点意见:

  • 当当前房间不是“休息室”时,代码会问两个问题:

“你想往哪个方向走?”“你想去哪里?”

这是不对的。第一个问题是不管你住在哪个房间,也不应该问特定房间的第二个问题。因此,从所有这些INPUT块中删除该ELIF语句.

  • 循环永远不会结束,而且由于指令中没有指示循环应该结束,这是很好的。但这也意味着程序中的最后两个语句永远不会执行:

如果currentRm =“游戏室”输出“游戏结束”

如果打算在用户进入游戏厅时显示该输出,并且程序可能应该结束,则需要在循环中移动这段代码,在该循环中,您已经检测到用户进入了该房间:

如果方向是‘东方’currentRm =‘游戏室’打印‘你是在游戏室’输出‘游戏超过’退出循环‘

  • ,您可以避免代码重复,并通过PRINT ‘You are in the ’, currentRm组合PRINT ‘You are in the Kitchen’的所有变体,并将该语句移动到对所有情况都执行该语句的位置。

在实际编程语言中,

  • 将创建一个图形数据结构,并避免使用这些IF..ELIF块。该数据结构的外观将取决于编程语言.

  • 您应该稍微改进代码的缩进。确保在IFELIF语句之后代码更缩进(4个空格是一个很好的标准),并且作为该块一部分的所有语句都在相同的缩进级别上。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72508235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档