免费基本计算器问题

时间:2014-03-21 00:43:50

标签: calculator freebasic

我试图用FreeBasic创建一个计算器。我的代码中line 9有什么问题? Line 6表示昏暗的选择是默认的,不允许line 9告诉我变量未声明。

1 declare sub main
2 declare sub add
3 declare sub subtract
4 main
5 sub main
6 dim choice
7 print "1.Add"
8 print "2.subtract"
9 input "what is your choice" ; choice

1 个答案:

答案 0 :(得分:0)

您的源代码非常不完整。例如,它错过了数据类型。在FreeBASIC中,您可以根据要存储的数据类型(IntegerDouble,String,...)选择多种数据类型。 此外,您没有定义sub programs实际应该如何工作。你没有给任何代码你的程序“减去”应该做什么。

以下是小型计算器的工作示例:

' These two functions take 2 Integers (..., -1, 0, 1, 2, ...) and 
' return 1 Integer as their result.
' Here we find the procedure declarations ("what inputs and outputs
' exist?"). The implementation ("how do these procedures actually
' work?") follows at the end of the program.
Declare Function Add (a As Integer, b As Integer) As Integer
Declare Function Subtract (a As Integer, b As Integer) As Integer

' == Begin of main program ==

Dim choice As Integer
Print "1. Add"
Print "2. Subtract"
Input "what is your choice? ", choice

' You could use "input" (see choice above) to get values from the user.
Dim As Integer x, y
x = 10
y = 6

If (choice = 1) Then
    Print Add(x, y)
ElseIf (choice = 2) Then
    Print Subtract(x, y)
Else
    Print "Invalid input!"
End If

Print "Press any key to quit."
GetKey
End

' == End of main program ==


' == Sub programs (procedures = subs and functions) from here on ==

Function Add (a As Integer, b As Integer) As Integer
    Return a + b
End Function

Function Subtract (a As Integer, b As Integer) As Integer
    Return a - b
End Function