0

我试图用 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
4

1 回答 1

0

您的源代码非常不完整。例如,它错过了数据类型。在 FreeBASIC 中,您可以根据要存储的数据类型在几种数据类型之间进行选择(整数双精度、字符串...)。此外,您没有定义子程序实际上应该如何工作。您没有提供任何代码,您的程序“减法”应该做什么。

这是您的小型计算器的一个工作示例:

' 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
于 2014-03-21T10:58:47.720 回答