vb编程,任意输入3个整数,按照由小到大排序输出.(代码纠错)

Private Sub Command1_Click()
a = Text1.Text
b = Text2.Text
c = Text3.Text
d = Text4.Text
Dim a, b, c As Integer
If a > b Then
If b > c Then
d = "c,b,a"
Else
If a > c Then
d = "b,c,a"
Else
d = "b,a,c"
End If
End If
Else
If b > c Then
If a > c Then
d = "c,a,b"
Else
d = "a,c,b"
End If
Else
d = "c,b,a"
End If
d = Text4.Text
End Sub

运行后出现“当前范围内的声明重复”,求教是哪边出错了==拜谢大神!!!

如果你的代码没有强制声明变量,那么,去掉
Dim a, b, c As Integer

如果有,改成
Dim a, b, c As Integer
Dim d As String
a = CInt(Text1.Text)
b = CInt(Text2.Text)
c = CInt(Text3.Text)

猜你是想显示排序的结果
d="c,b,a"
应该改成
d=c1 & "," & b1 & "," & a1

另外,最后一行
d = Text4.Text

应该改为
Text4.Text=d

还有,不用这么麻烦
可用
a1=workshrrtfunction.max(a,b,c)
c1=worksheetfunction.min(a,b,c)
b1=a+b+c-a1-c1
d=c1 & "," & b1 & "," & a1追问

workshrrtfunction.max,c1 & 这些个都没教到啊。。

追答

通过workshrrtfunction来引用EXCEl表的函数,max就是EXCEL表中“插入函数”内的max,用来求最大值

min同样也是表函数,用来求最小值
& 符号表示连接字符,与EXCEL中连接字符是一样的

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-14
你这代码都 错的离谱了 没法改了
看我给你上一段吧 符合你的要求
Dim a(4) As Double
Dim b As Double
Private Sub Command1_Click()
a(1) = Val(Text1.Text)
a(2) = Val(Text2.Text)
a(3) = Val(Text3.Text)
a(4) = Val(Text4.Text)
For i = 1 To 2
For j = i To 3
If a(i) > a(j) Then
b = a(i)
a(i) = a(j)
a(j) = b
End If
Next j
Next i

Text4.Text = Str(a(1)) & Str(a(2)) & Str(a(3))
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub追问

可是我们还没有学到i,j,sra什么的。。。

第2个回答  2013-10-14
Private Sub Command1_Click()
Dim a, b, c As Integer
a = val(Text1.Text)
b = val(Text2.Text)
c = val(Text3.Text)
d = val(Text4.Text)
If a > b Then
If b > c Then
d = "c,b,a"
Else
If a > c Then
d = "b,c,a"
Else
d = "b,a,c"
End If
End If
Else
If b > c Then
If a > c Then
d = "c,a,b"
Else
d = "a,c,b"
End If
Else
d = "c,b,a"
End If
Text4.Text = d
End Sub
注意,你要检查在(通用)或全局变量里有没有设置a,b,c。
第3个回答  2013-10-14
Private Sub Command1_Click()
   a = Text1.Text
   b = Text2.Text
   c = Text3.Text
   Dim a, b, c As Integer
    If a > b Then t = a: a = b: b = t
    If a > c Then t = a: a = c: c = t
    If b > c Then t = b: b = c: c = t
    Text4.Text = a & "," & b & "," & c
End Sub

从小到大排序输出

第4个回答  2013-10-14
你原来的代码存在三个方面的错误:
1、“当前范围内的声明重复”这实际上是由于“先使用变量后声明”造成的。先使用如:
a = Text1.Text ,这时候VB自动认为你定义了a为字符串变量。所以,当你进行声明
Dim a, b, c As Integer
的时候,就发生了“当前范围内的声明重复”,因为a已经隐式声明过了。b、c也一样。
解决办法就是:将声明语句放在变量使用之前。改成这样:
Dim a, b, c As Integer
a = Text1.Text
b = Text2.Text
c = Text3.Text
d = Text4.Text
2、末尾的“d = Text4.Text”,应该是你将赋值方向搞反了,本来是要输出的,却变成了“输入”。
3、其中的一些判断,发生了混乱。

根据以上问题,对你的代码修改如下:

Private Sub Command1_Click()
Dim a, b, c As Integer
a = Text1.Text
b = Text2.Text
c = Text3.Text
d = Text4.Text
If a > b Then
If b > c Then
d = "a,b,c"
Else
If a > c Then
d = "a,c,b"
Else
d = "c,a,b"
End If
End If
Else
If b > c Then
If a > c Then
d = "b,a,c"
Else
d = "b,c,a"
End If
Else
d = "c,b,a"
End If
End If
Text4.Text = d
End Sub

OK!运行成功了!本回答被提问者采纳
相似回答