vb dim中两个变量同一行定义与不同行定义的区别

来源:百度知道 编辑:UC知道 时间:2024/05/20 00:36:36
如dim a,b,c as integer与
dim a as integer
dim b as integer
dim c as integer
究竟有什么区别呢?实践证明是有区别的.但是原因不懂,还请高手给予指点.
dim x as integer
dim max as integer
dim i as integer
max=inputbox("请输入10个数")
for i=1 to 9
x=inputbox("请输入10个数")
当我输入1,2,3,4,5,6,7,8,9,10时输出为10 (答案为对的)
而这段程序
Dim x, max, i As Integer
max = InputBox("请输入10个数")
For i = 1 To 9
x = InputBox("请输入10个数")
If x > max Then max = x
Next i
Print "10个数中的最大值为:"
if x>max then max=x
next i
print "10个数中的最大值为:", max
当我输入1,2,3,4,5,6,7,8,9,10时输出为9(答案为错的)

能具体说说可变型是何意吗?有何规律吗?一般情况好像没有影响的.

dim a,b,c as integer表示c是integer型,a,b是可变型.
如果要达到三行的效果,就应该这样写:
dim a as integer,b as integer,c as integer
这样就和写三行是一样的
我的回答你没看懂吗?
max=inputbox("请输入10个数") ,由于max是可变型,因此在这句里,VB自动把它当成string 型,max="10",这时10是字符串,而不是数。同样"9"也不是数,而在字符串中,"9">"10",所以得不到你要的结果。

  1. VB中声明变量多个变量同一行写法必须写成:

    Dim a As long1, b As Long, c As Integer

    如果这样写:

    Dim a As long1, b As Long, c As Integer

    a和b被声明成变体变量类型,仅c声明为 Integer数据类型。

  2. 如果用多行声明语句声明则不会出现上述情况。

    Dim a As Long

    Dim b As Long

    Dim c As Integer