如何在BASIC中将变量指定为整数?

时间:2021-03-03 11:17:10

So I'm writing a very basic 'Game' Program, just really started coding today, and am running into a problem. The tutorial I'm following, at http://home.cmit.net/rwolbeck/programmingtutorial/index.htm suggests that I do not need to specify that a variable is an integer variable, but whether I do or don't, BlitzMax stops compiling midway with an error message of "Compile Error: Unable to convert 'String' to 'Int'"

所以我正在编写一个非常基本的“游戏”程序,刚刚开始编写代码,并且遇到了问题。我正在关注的教程,http://home.cmit.net/rwolbeck/programmingtutorial/index.htm表明我不需要指定一个变量是一个整数变量,但无论我是否做到, BlitzMax中途停止编译,出现“编译错误:无法将'字符串'转换为'Int'的错误消息”

name$ = Input("What is your name? ")
Print "Hello " + name$
answer = Input("What is 2 and 2? ")

If name$ = "Kyle"
    Print "Kyle is always right."
    WaitKey()
    End
EndIf

If answer = 4
    Print "No, 2 and 2 is 22."
Else
    Print "No, 2 and 2 is 4."
EndIf

WaitKey()
End

Any ideas of what is going wrong, and how to fix it?

什么是错误的想法,以及如何解决它?

P.S. Just joined this site today but I did do multiple searches and didn't find anything answering this specific question/problem.

附:刚刚加入这个网站,但我做了多次搜索,没有找到任何回答这个具体问题的问题。

1 个解决方案

#1


This is the problem line:

这是问题所在:

answer = Input("What is 2 and 2? ")

Your lack of a "$" on your "answer" variable tells it that it's an integer (in classic Dartmouth BASIC that would be a floating point, with "%" designating an integer). The INPUT function returns a string, which can't be stuffed into a number. Use the VAL function to do the type conversion, i.e.

你的“回答”变量缺少“$”表示它是一个整数(在经典的达特茅斯BASIC中,它将是一个浮点,“%”表示一个整数)。 INPUT函数返回一个字符串,该字符串不能填入数字。使用VAL函数进行类型转换,即

answer = Val(Input("What is 2 and 2? "))

#1


This is the problem line:

这是问题所在:

answer = Input("What is 2 and 2? ")

Your lack of a "$" on your "answer" variable tells it that it's an integer (in classic Dartmouth BASIC that would be a floating point, with "%" designating an integer). The INPUT function returns a string, which can't be stuffed into a number. Use the VAL function to do the type conversion, i.e.

你的“回答”变量缺少“$”表示它是一个整数(在经典的达特茅斯BASIC中,它将是一个浮点,“%”表示一个整数)。 INPUT函数返回一个字符串,该字符串不能填入数字。使用VAL函数进行类型转换,即

answer = Val(Input("What is 2 and 2? "))