2015-05-11 37 views
-1

我一直在学习FreeBasic并开始编写井基本文本冒险游戏。我一直遵循相同的公式,但根据我最近的发展获得了以下错误:FreeBasic:错误32:预计'结束'如果'结束'

beta.bas(226) error 32: Expected 'END IF' in 'end'

这很奇怪,因为以前的一切工作都很好。这里是我的代码 -

的两个副本Broken-

Dim As String strInput 

main: 
    print "You have awakened in a dark room. What do you do? " 
    input ">", strInput 
    sleep 500 

    if strinput = "Leave" then 
     goto option1L1 
    else if strinput = "leave" then 
     goto option1L1 
    else 
     print "You can't do that." 
    end if 
    sleep 500 
    goto main 

option1L1: 
    print "You have left the room" 
    sleep 500 
    print "You are now outside. What do you want to do.?" 
    input ">", strInput 
    sleep 500 
    if strinput = "Look around" then 
     goto lookaround1 
    else if strinput = "look around" then 
     goto lookaround1 
    else 
     print "You can't do that." 
    end if 
    goto option1L1 

lookaround1: 
    print "You see a road leading into a dark forest, and a road leading into a happy looking meadow. Which path do you take?" 
    sleep 500 
    input ">", strinput 
    if strinput = "Dark forest" then 
     goto darkforest 
    else if strinput = "dark forest" then 
     goto darkforest 
    else if strinput = "Dark Forest" then 
     goto darkforest 
    else if strinput = "Happy meadow" then 
     goto happymeadow 
    else if strinput = "Happy Meadow" then 
     goto happymeadow 
    else if strinput = "happy meadow" then 
     goto happymeadow 
    else if strinput = "happy looking meadow" then 
     goto happymeadow 
    else if strinput = "Happy looking meadow" then 
     goto happymeadow 
    else if strinput = "Happy Looking Meadow" then 
     goto happymeadow 
    else 
     print "You can't do that." 
    end if 
    goto lookaround1 
    end if 
    end 

darkforest: 
    print "You have taken the path leading into the dark forest. Before you, you see a bear. What do you do?" 
    print "Options- 'Attack bear' or 'Run from bear'" 
    print "Tip- Commands are case sensitive" 
    sleep 500 
    input ">", strinput 
    if strinput = "Attack bear" then 
     goto attackbear 
    else if strinput = "Run from bear" then 
     goto runfrombear 
    else 
     print "You can't do that." 
    END if 
    goto darkforest 
    END 

happymeadow: 
    print "The meadow turns out to be a trap. Unfortunately, you are caught in this trap and are now dead. Goodbye." 
    END if 
    end 

attackbear: 
    print "You attack the bear with your 'bare' hands. You prevail, but have broken your arm. What do you do?" 
    print "Your options are to 'Make a splint' or to 'Press onward'" 
    sleep 500 
    input ">", strinput 
    if strinput = "Make a splint" then 
     goto makeasplint 
    else if strinput = "Press onward" then 
     goto pressonward 
    else 
     print "You can't do that." 
    END if 
    goto attackbear 
    end if 
    END 

runfrombear: 

    print "You tried to run from the bear, but were eaten. Sorry!" 
    END if 
    end 

makeasplint: 
    print "You made a splint, and are able to carry on." 
    print "Travelling..." 
    sleep 1000 
    print "You have come to a small house. What do you do?" 
    print "Options- 'Go inside', 'Wait outside, 'Keep travelling'" 
    input ">", strinput 
    if strinput = "Go inside" then 
     goto insidehouse 
    else if strinput = "Wait outside" then 
     goto outsidehouse 
    else if strinput = "Keep travelling" then 
     goto keeptravelling 
    else 
     print "You can't do that." 
     print "Tip: Commands are case sensitive" 
     goto makeasplint 
    END if 
    end 

pressonward: 
    print "You press onward, but trip on a rock and die." 
    END if 
    end 

insidehouse: 
    print "You have entered the cabin. Inside you find a sword. Do you want to equip the sword?" 
    print "Options: 'Yes' or 'No'" 
    input ">", strinput 
    if strinput = "Yes" then 
     goto yes 
    else if strinput = "No" then 
     goto no 
    else 
     print "Please enter either 'Yes' or 'No'" 
     print "Remember, all commands are case sensitive. Sorry!" 
     goto insidehouse 

    end if 
    end 

outsidehouse: 
    print "You wait around outside the huse. You eventually die of starvation." 
    end if 
    end 

keeptravelling: 
    print "You keep travelling and eventually find your way out of the forest. " 
    print "Travelling..." 
    sleep 2000 
    print "You have now come to a giant castle. What do you want to do?" 
    print "Your options are 'Go inside' and 'Leave'" 
    input ">", strinput 
    if strinput = "Go inside" then 
     goto insidecastle 
    else if strinput = "Leave" then 
     goto leave 
    else 
     print "You can't do that" 
     goto keeptravelling 
    END if 
    end 

yes: 
    print "You equip the sword, but slip and stab yourself with it. You are dead." 
    end if 
    end 

no: 
    print "You leave the house." 
    goto makeasplint 
    end if 
    end 


insidecastle: 
    print "After entering the castle, you slip on a stone and hit your face on the floor, rendering you unconscious." 
    print "..." 
    sleep 2000 
    print "After you awake, you find your movement to be constricted by some sort of leather strap." 
    print "..." 
    sleep 2000 
    print "You hear a sound. It is a rough voice saying 'This is my swamp..." 
    print "The ogre kills you, and you are dead." 
    end if 
    end 

leave: 
    print "You leave the castle, and find your way to a happy cottage, where you find a bed. What do you do now?" 
    print "Your options are 'Sleep' and 'Don't sleep'" 
    input ">", strinput 
    if strinput = "Sleep" then 
     goto sleeping 
    else if strinput = "Don't sleep" then 
     goto dontsleep 
    else 
     print "You can't do that." 
     goto leave 
    end if 
    end 

sleeping: 
    print "You die in your sleep." 
    end if 
    end 

dontsleep: 
    print "By not sleeping, you avoided the ghosts. Congrats, you've survived!" 
    sleep 5000 
    print "Do you wish to exit?" 
    input ">", strinput 
    if strinput = "Yes" then 
     goto exitgame 
    if strinput = "No" then 
     print "Too bad!" 
     goto exitgame 
    end if 
    end 
exitgame: 
    end 

而且要清楚,我尝试添加一个“结束,如果”中的“结束”有过,但它还是给了我:

"expected 'end if' in 'end if'"

末working-

Dim As String strInput 

main: 
    print "You have awakened in a dark room. What do you do? " 
    input ">", strInput 
    sleep 500 

    if strinput = "Leave" then 
     goto option1L1 
    else if strinput = "leave" then 
     goto option1L1 
    else 
     print "You can't do that." 
    end if 
    sleep 500 
    goto main 

option1L1: 
    print "You have left the room" 
    sleep 500 
    print "You are now outside. What do you want to do.?" 
    input ">", strInput 
    sleep 500 
    if strinput = "Look around" then 
     goto lookaround1 
    else 
     print "You can't do that." 
    end if 
    goto option1L1 

lookaround1: 
    print "You see a road leading into a dark forest, and a road leading into a happy looking meadow. Which path do you take?" 
    sleep 500 
    input ">", strinput 
    if strinput = "Dark forest" then 
     goto darkforest 
    else if strinput = "Happy meadow" then 
     goto happymeadow 
    else 
     print "You can't do that." 
    end if 
    goto lookaround1 
    end 


darkforest: 
    print "You have taken the path leading into the dark forest. Before you, you see a bear. What do you do?" 
    print "Options- 'Attack bear' or 'Run from bear'" 
    print "Tip- Commands are case sensitive" 
    sleep 500 
    input ">", strinput 
    if strinput = "Attack bear" then 
     goto attackbear 
    else if strinput = "Run from bear" then 
     goto runfrombear 
    else 
     print "You can't do that." 
    END if 
    goto darkforest 
    END 

happymeadow: 
    print "The meadow turns out to be a trap. Unfortunately, you are caught in this trap and are now dead. Goodbye." 
    END if 
    end 


attackbear: 
    print "You attack the bear with your 'bare' hands. You prevail, but have broken your arm. What do you do?" 
    print "Your options are to 'Make a splint' or to 'Press onward'" 
    sleep 500 
    input ">", strinput 
    if strinput = "Make a splint" then 
     goto makeasplint 
    else if strinput = "Press onward" then 
     goto pressonward 
    else 
     print "You can't do that." 
    END if 
    goto attackbear 
    END 

runfrombear: 

    print "You tried to run from the bear, but were eaten. Sorry!" 
    END if 
    end 

makeasplint: 
    print "You made a splint, and are able to carry on." 
    print "Travelling..." 
    sleep 1000 
    print "You have come to a small house. What do you do?" 
    print "Options- 'Go inside', 'Wait outside, 'Keep travelling'" 
    input ">", strinput 
    if strinput = "Go inside" then 
     goto insidehouse 
    else if strinput = "Wait outside" then 
     goto outsidehouse 
    else if strinput = "Keep travelling" then 
     goto keeptravelling 
    else 
     print "You can't do that." 
     print "Tip: Commands are case sensitive" 
     goto makeasplint 
    END if 
    end 

pressonward: 
    print "You press onward, but trip on a rock and die." 
    END if 
    end 

insidehouse: 
    print "You have entered the cabin. Inside you find a sword. Do you want to equip the sword?" 
    print "Options: 'Yes' or 'No'" 
    input ">", strinput 
    if strinput = "Yes" then 
     goto yes 
    else if strinput = "No" then 
     goto no 
    else 
     print "Please enter either 'Yes' or 'No'" 
     print "Remember, all commands are case sensitive. Sorry!" 
     goto insidehouse 

    end if 
    end 

outsidehouse: 
    print "You wait around outside the huse. You eventually die of starvation." 
    end if 
    end 

keeptravelling: 
    print "You keep travelling and eventually find your way out of the forest. " 
    print "Travelling..." 
    sleep 2000 
    print "You have now come to a giant castle. What do you want to do?" 
    print "Your options are 'Go inside' and 'Leave'" 
    input ">", strinput 
    if strinput = "Go inside" then 
     goto insidecastle 
    else if strinput = "Leave" then 
     goto leave 
    else 
     print "You can't do that" 
     goto keeptravelling 
    END if 
    end 

yes: 
    print "You equip the sword, but slip and stab yourself with it. You are dead." 
    end if 
    end 

no: 
    print "You leave the house." 
    goto makeasplint 
    end if 
    end 


insidecastle: 
    print "After entering the castle, you slip on a stone and hit your face on the floor, rendering you unconscious." 
    print "..." 
    sleep 2000 
    print "After you awake, you find your movement to be constricted by some sort of leather strap." 
    print "..." 
    sleep 2000 
    print "You hear a sound. It is a rough voice saying 'This is my swamp..." 
    print "The ogre kills you, and you are dead." 
    end if 
    end 

leave: 
    print "You leave the castle, and find your way to a happy cottage, where you find a bed. What do you do now?" 
    print "Your options are 'Sleep' and 'Don't sleep'" 
    input ">", strinput 
    if strinput = "Sleep" then 
     goto sleeping 
    else if strinput = "Don't sleep" then 
     goto dontsleep 
    else 
     print "You can't do that." 
     goto leave 
    end if 
    end 

sleeping: 
    print "You die in your sleep." 
    END if 
    end 

dontsleep: 
    print "By not sleeping, you avoided the ghosts. Congrats, you've survived!" 
    end if 
    end 

我在所有这一切都相当新手,我非常为难。我究竟做错了什么?

+1

如果你缩进代码多一点,错误,......智能化,你可能会立即看到你的问题。没有违法意图。 – paxdiablo

+0

我想;当一个'GOTO'调用并且编译器离开一个'IF'块时,你不需要任何'END IF'来完成那个'IF'块,你需要一个'END IF'来完成一个'IF'块; )。 –

回答

1

您正在使用过时的编程风格 - 例如,你用GOTO使用了很多跳转。 FreeBasic提供了很多更好的概念来构造代码:函数/子集,用户定义的类型,类,继承...

尽管如此,代码中存在导致编译错误的一些基本错误。主要的一点是你使用“Else If”而不是“ElseIf”(注意空格字符)。另一个是你使用“End If”,尽管你还没有使用“If”或“ElseIf”开始一个IF块。

下面是你的代码的一个版本,它在语法上是正确的(它将被编译),但仍然使用错误的代码风格(例如GOTO和标签)。另外还有语义和其他问题。例如。一个END在GOTO之后没有意义,因为它永远不会到达。

Dim As String strInput 

main: 
    print "You have awakened in a dark room. What do you do? " 
    input ">", strInput 
    sleep 500 

    if strinput = "Leave" then 
     goto option1L1 
    ElseIf strinput = "leave" then 
     goto option1L1 
    else 
     print "You can't do that." 
    end if 
    sleep 500 
    goto main 

option1L1: 
    print "You have left the room" 
    sleep 500 
    print "You are now outside. What do you want to do.?" 
    input ">", strInput 
    sleep 500 
    if strinput = "Look around" then 
     goto lookaround1 
    ElseIf strinput = "look around" then 
     goto lookaround1 
    else 
     print "You can't do that." 
    end if 
    goto option1L1 

lookaround1: 
    print "You see a road leading into a dark forest, and a road leading into a happy looking meadow. Which path do you take?" 
    sleep 500 
    input ">", strinput 
    if strinput = "Dark forest" then 
     goto darkforest 
    ElseIf strinput = "dark forest" then 
     goto darkforest 
    ElseIf strinput = "Dark Forest" then 
     goto darkforest 
    ElseIf strinput = "Happy meadow" then 
     goto happymeadow 
    ElseIf strinput = "Happy Meadow" then 
     goto happymeadow 
    ElseIf strinput = "happy meadow" then 
     goto happymeadow 
    ElseIf strinput = "happy looking meadow" then 
     goto happymeadow 
    ElseIf strinput = "Happy looking meadow" then 
     goto happymeadow 
    ElseIf strinput = "Happy Looking Meadow" then 
     goto happymeadow 
    else 
     print "You can't do that." 
    end if 
    goto lookaround1 
    end 

darkforest: 
    print "You have taken the path leading into the dark forest. Before you, you see a bear. What do you do?" 
    print "Options- 'Attack bear' or 'Run from bear'" 
    print "Tip- Commands are case sensitive" 
    sleep 500 
    input ">", strinput 
    if strinput = "Attack bear" then 
     goto attackbear 
    ElseIf strinput = "Run from bear" then 
     goto runfrombear 
    else 
     print "You can't do that." 
    END if 
    goto darkforest 
    END 

happymeadow: 
    print "The meadow turns out to be a trap. Unfortunately, you are caught in this trap and are now dead. Goodbye." 
    end 

attackbear: 
    print "You attack the bear with your 'bare' hands. You prevail, but have broken your arm. What do you do?" 
    print "Your options are to 'Make a splint' or to 'Press onward'" 
    sleep 500 
    input ">", strinput 
    if strinput = "Make a splint" then 
     goto makeasplint 
    ElseIf strinput = "Press onward" then 
     goto pressonward 
    else 
     print "You can't do that." 
    END if 
    goto attackbear 
    END 

runfrombear: 
    print "You tried to run from the bear, but were eaten. Sorry!" 
    end 

makeasplint: 
    print "You made a splint, and are able to carry on." 
    print "Travelling..." 
    sleep 1000 
    print "You have come to a small house. What do you do?" 
    print "Options- 'Go inside', 'Wait outside, 'Keep travelling'" 
    input ">", strinput 
    if strinput = "Go inside" then 
     goto insidehouse 
    ElseIf strinput = "Wait outside" then 
     goto outsidehouse 
    ElseIf strinput = "Keep travelling" then 
     goto keeptravelling 
    else 
     print "You can't do that." 
     print "Tip: Commands are case sensitive" 
     goto makeasplint 
    END if 
    end 

pressonward: 
    print "You press onward, but trip on a rock and die." 
    end 

insidehouse: 
    print "You have entered the cabin. Inside you find a sword. Do you want to equip the sword?" 
    print "Options: 'Yes' or 'No'" 
    input ">", strinput 
    if strinput = "Yes" then 
     goto yes 
    ElseIf strinput = "No" then 
     goto no 
    else 
     print "Please enter either 'Yes' or 'No'" 
     print "Remember, all commands are case sensitive. Sorry!" 
     goto insidehouse 
    end if 
    end 

outsidehouse: 
    print "You wait around outside the huse. You eventually die of starvation." 
    end 

keeptravelling: 
    print "You keep travelling and eventually find your way out of the forest. " 
    print "Travelling..." 
    sleep 2000 
    print "You have now come to a giant castle. What do you want to do?" 
    print "Your options are 'Go inside' and 'Leave'" 
    input ">", strinput 
    if strinput = "Go inside" then 
     goto insidecastle 
    ElseIf strinput = "Leave" then 
     goto leave 
    else 
     print "You can't do that" 
     goto keeptravelling 
    END if 
    end 

yes: 
    print "You equip the sword, but slip and stab yourself with it. You are dead." 
    end 

no: 
    print "You leave the house." 
    goto makeasplint 
    end 


insidecastle: 
    print "After entering the castle, you slip on a stone and hit your face on the floor, rendering you unconscious." 
    print "..." 
    sleep 2000 
    print "After you awake, you find your movement to be constricted by some sort of leather strap." 
    print "..." 
    sleep 2000 
    print "You hear a sound. It is a rough voice saying 'This is my swamp..." 
    print "The ogre kills you, and you are dead." 
    end 

leave: 
    print "You leave the castle, and find your way to a happy cottage, where you find a bed. What do you do now?" 
    print "Your options are 'Sleep' and 'Don't sleep'" 
    input ">", strinput 
    if strinput = "Sleep" then 
     goto sleeping 
    ElseIf strinput = "Don't sleep" then 
     goto dontsleep 
    else 
     print "You can't do that." 
     goto leave 
    end if 
    end 

sleeping: 
    print "You die in your sleep." 
    end 

dontsleep: 
    print "By not sleeping, you avoided the ghosts. Congrats, you've survived!" 
    sleep 5000 
    print "Do you wish to exit?" 
    input ">", strinput 
    if strinput = "Yes" then 
     goto exitgame 
    ElseIf strinput = "No" then 
     print "Too bad!" 
     goto exitgame 
    end if 
    end 

exitgame: 
    end