用VB编写一个程序,三个数比较大小的问题

作者&投稿:粱羽 (若有异议请与网页底部的电邮联系)
用VB编写三个数比较大小的程序~

给你一个通用程序:
private sub form_load()
from1.autoredraw=true
endsub

private sub form_click()
dim Shu(3) as integer,Xu(3) as string,I as integer,J as integer
shu(1)=a:shu(2)=b:shu(3)=c
xu(1)="a":xu(2)="b":xu(3)="c"
for i=1 to 2
for J=I+1 to 3
if shu(i)<shu(j) then
shu(0)=shu(i):xu(0)=xu(i)
shu(i)=shu(j):xu(i)=xu(j)
shu(j)=shu(0):xu(j)=xu(0)
end if
next
next
xu(0)=""
for i=1 to 2
xu(0)=xu(0)+xu(i)
if shu(i)>shu(i+1) then
xu(0)=xu(0)+">"
else
xu(0)=xu(0)+"="
end if
xu(0)=xu(0)+xu(i)
print xu(0)
endsub

可以用VB的 IIf 函数:虽然方便,但是比较抽象,难以理解:
不如要得到a,b,c中的最小值:
Print IIf(IIf(a < b, a, b) < IIf(b < c, b, c), IIf(a < b, a, b), IIf(b < c, b, c))
把其中的小于号改为大于号。就可以得到三个数中的最大值。

你们的代码都属于新手看不懂的 呵呵 我给你写了一个比较容易懂的! 先和你说一下原理
首先我弄了4个输入框控件(text) 一个按钮 输入框1 在代码中为 A , 输入框2 为B,3为C, 4为D ! 前三个 是比大小的输入数 输入框4也就是D 为显示最大数的! 原理为:
a和B先对比 如果A大于B 然后 D等于A (输入框现实的是最大A!) 否则显示最大B
再用C和D对比(由于D已经等于了A或B的值!所以就拿D和C对比了)如果C大 然后D等于C
如果C小 不做反应!也就是没有否则!下面是代码:
Private Sub Command1_Click()
Dim a, b, c, d As Single
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
d = Val(Text4.Text)
If a > b Then
Text4.Text = a
Else
Text4.Text = b
End If
If c > d Then
Text4.Text = c
End If
End Sub
废话有点多,其实我可以不说原理的 只是怕新手看不懂的 呵呵 !!

相信楼主也会一些基本的操作了,好,我简要地说一下步骤,同时给出代码:
1、新建一个工程,窗体form1上新建三个文本框,一个按钮
2、在代码窗口输入如下代码(直接复制粘贴即可):

Private Sub Command1_Click()
Dim Datas(3) As Double
Dim temp

On Error Resume Next '容错语句
'将三个文本框中的数据依次赋给数组中的三个元素
Datas(1) = Text1.Text
Datas(2) = Text2.Text
Datas(3) = Text3.Text

'下面对数组中的元素进行排序
For i = 1 To 2
'遍历数组,对不符合大小关系的元素进行倒置
For j = i + 1 To 3
If Datas(i) < Datas(j) Then
temp = Datas(i)
Datas(i) = Datas(j)
Datas(j) = temp
End If
Next
Next

'下面输出排序结果
Text1.Text = Datas(1)
Text2.Text = Datas(2)
Text3.Text = Datas(3)
End Sub

Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Command1.Caption = "排序"
End Sub

好了,你可以在文本框上输入任何数据,然后点击“排序”按钮就成功了。 此外,将本例中的循环语句进行二次扩展以后就可以处理三个元素以上(甚至多维数组、矩阵)的复杂排序问题。

Private Sub Command1_Click()
Dim s(3) As Long
Dim p(3) As Long
Dim lj As Long
s(1) = InputBox("第一个数", "请输入")
s(2) = InputBox("第二个数", "请输入")
s(3) = InputBox("第三个数", "请输入")
lj = 1
If s(1) > s(2) Then lj = lj + 1
If s(1) > s(3) Then lj = lj + 1
If p(lj) <> 0 Then lj = lj + 1
p(lj) = s(1): lj = 1
If s(2) > s(1) Then lj = lj + 1
If s(2) > s(3) Then lj = lj + 1
If p(lj) <> 0 Then lj = lj + 1
p(lj) = s(2): lj = 1
If s(3) > s(1) Then lj = lj + 1
If s(3) > s(2) Then lj = lj + 1
If p(lj) <> 0 Then lj = lj + 1
p(lj) = s(3): lj = 1
Print p(1), p(2), p(3)
End Sub

x,y,z
if x>y then
t=x
x=y
y=t
end if
if x>z then
t=x
x=z
z=t
end if
if y>z then
t=y
y=z
z=t
end if
pring x,y,z


在VB中,编写一个程序,计算15!-8!+5!的值。
当键盘输入n,表示求该式的前n项的和。比如输入3,就是求:1*3+2*4+3*5 比如输入2,就是求:1*3+2*4 所以代码为:dim n as integer dim s as double dim i as integer n = Val(InputBox("输入一个正整数!"))s =0 for i = 1 to n s = s + i*(i+2)next i Print s ...

用VB编写一个计算器程序的代码
1、创建控件组的方法首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption 属性为数字 0 ;然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。这时,第一个按钮的Index...

如何使用VB编写一个简单的小程序?
在桌面上,鼠标左键双击程序,在打开的VB6主界面上,左边是工具菜单栏,单击标签按钮,在Form1窗口上,绘制出一Label1,然后在其属性窗口上改个名字,如Caption为第一数,如下图所示。接着,用同样的方法,在Form1窗口上绘制出第二数,并且在左边工具菜单栏,单击文本框按钮,并绘制出来。在文本框属性...

VB编写程序的步骤
VB编写程序的步骤可以分为三步:设计程序的界面,根据想要实现的功能画出程序的界面;编写代码是最枯燥的一步,根据程序的功能,为每个控件元素编写代码,这是程序设计的关键所在;发布应用程序为程序创建安装项目。

如何用VB编程实现水仙花数?
1、启动VB程序,新建一个标准exe工程 2、在窗体上绘制一个命令按钮(名称:Command),双击命令按钮进入代码窗口。3、接下来开始编写命令按钮的单击事件。点击事件要实现的内容就是上面提供的程序代码。4、按F5运行程序,点击命令命令按钮,输出的水仙花数共有4个:153,370,371,407。

vb编写hello world 程序
vb编写hello world 程序可以参考下面的代码:Private Sub Form_Load()MsgBox ("Hello World")End Sub 在窗体上单击即可运行程序。

用VB语言实现一个小程序
小程序很简单,不用控件,代码如下:Dim A(4) As String, B(4) As Integer, C(4) As String, St As StringDim I As Integer, J As Integer, H1 As Integer, H2 As Integer Private Sub Form_Load() Form1.AutoRedraw = True A(1) = "葡萄牙" A(2) = "西班牙" A(...

用VB写一个小程序.
vb6代码如下,添加模块,工程--属性--启动对象选择sub main Sub main()Open "c:\\temp\\25.txt" For Input As #1 Clipboard.Clear Clipboard.SetText StrConv(InputB(LOF(1), 1), vbUnicode)End Sub

用VB写设计程序代码,求:s=1+(1+2)+(1+2+3)+……+(1+2+3+……+n)的值...
Function sumD(n As Integer) As Single s = 1 i = 1 Do i = i + 1 s = s + 1 \/ i Loop Until i >= n sumD = s End Function Function sumF(n As Integer) As Single s = 0 For i = 1 To n s = s + 1 \/ i Next i sumF = s End Function Private Sub Form_...

VB写一个小程序1.exe,1.exe读取form1的文本框内容。或者直接将form1的...
Dim h1 As Long Dim h2 As Long Dim Txt(64000) As Byte Dim strControl As String * 255 h1 = FindWindow("ThunderRT6FormDC", "Form1") '父系窗口句柄 ' h1 = FindWindow(vbNullString, "窗口名称")' h1 = FindWindow("窗口类名", vbNullString) '这三个h1效果一样 h2 = ...

巴青县17234995601: 用VB编写三个数比较大小的程序
莱洪缬沙: private sub form1_click() dim a(3) As Integer,I As Integer,J As Integer,B as boolean form1.autoredraw=true a(1)=50 a(2)=9 A(3)=11 print "三个数:" for i=1 to 3 print a(i); next print For I=1 to 3 B=false for J=1 to I-1 if a(j)&gt;a(j+1) then a(0)=a(j) a(j)...

巴青县17234995601: 用VB编写三个数比较大小的程序 -
莱洪缬沙: 给你一个通用程序:private sub form_load() from1.autoredraw=true endsub private sub form_click() dim Shu(3) as integer,Xu(3) as string,I as integer,J as integer shu(1)=a:shu(2)=b:shu(3)=c xu(1)="a":xu(2)="b":xu(3)="c" for i=1 to 2 for J=I+1 ...

巴青县17234995601: vb比较三个数的大小程序 -
莱洪缬沙: private sub form_click() dim a as integer, b as integer, c as integer a = inputbox("请输入一个整数:") b = inputbox("请输入一个整数:") c = inputbox("请输入一个整数:") print a, b, c if a

巴青县17234995601: vb 比较3个数的大小 -
莱洪缬沙: 另一种加文本框比较方法 首先在VB添加三个文本框(TEXT) 和一个命令按钮(COMMAND) Private Sub COMMAND1_CLICK()'这里是CLICK不是LOAD,看清除 Dim A As Integer '定义三个数 Dim B As Integer Dim C As Integer A = Val(Text...

巴青县17234995601: vb中三个数比较大小 -
莱洪缬沙: 建Command1,代码如下.================ Option Explicit Private Sub Command1_Click() Dim a As Integer, b As Integer, c As Integer, t As Integer a = Val(InputBox("a=?")) b = Val(InputBox("b=?")) c = Val(InputBox("c=?")) If a > b ...

巴青县17234995601: 用VB编程来比较三个数的大小 -
莱洪缬沙: 是自学的吗如果 a>b 那么max=a 否则max=b 此时max是a和b中的最大值接下来只需让max和c进行比较就可以得出最大值具体代码不给你了,思路有了自己去编比较好

巴青县17234995601: VB语言任意三个数比较大小的程序 -
莱洪缬沙: a[]; temp; if(a[0]temp=a[0]; a[0]=a[1]; a[1]=temp; } if(a[2]>a[0]){ temp=a[0]; a[0]=a[2]; a[2]=a[1]; a[1]=temp; }else{ if(a[2]>a[1]){ temp=a[1]; a[1]=a[2]; a[2]=temp; }} a[]就是一个从大到小的排序数组

巴青县17234995601: 用vb程序怎样实现把三个数字进行大小的比较 -
莱洪缬沙: Private Sub Form_Click() Dim a, b, c, d As Integer a = Val(InputBox("a=")) b = Val(InputBox("b=")) c = Val(InputBox("c=")) If a If b If a Print a; b; c End Sub

巴青县17234995601: 用VB编写一个程序,三个数比较大小的问题 -
莱洪缬沙: 你们的代码都属于新手看不懂的 呵呵 我给你写了一个比较容易懂的! 先和你说一下原理 首先我弄了4个输入框控件(text) 一个按钮 输入框1 在代码中为 A , 输入框2 为B,3为C, 4为D ! 前三个 是比大小的输入数 输入框4也就是D 为显示最大...

巴青县17234995601: VB语言任意三个数比较大小的程序
莱洪缬沙: 人家要VB,你写的是什么? Dim a, b, c, temp As Integer a = Text1.Text b = Text2.Text c = Text3.Text If c > b Then temp = b b = c c = temp End If If b > a Then temp = a a = b b = temp End If If c > a Then temp = a a = c c = temp End If Print a & ">" & b & ">" & c

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网