关于循环的作业。给100分,请高手帮忙!

xmzorro 2003-10-30 11:59:38
在text1.text输入一个数,比较该数是否已经在数组中,如果在则删除数组中的这个数,如果没有则赋值给该数组中的任一数,并对数组进行从小到大排序后输出在text2.text。
...全文
105 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
raisuny 2003-11-02
  • 打赏
  • 举报
回复
不会吧?这么一个简单的问题,都到CSDN上来讨论,并且来这么多的讨论者。
其实在数据结构中一个很简单的结构就可以实现。
xmzorro 2003-11-02
  • 打赏
  • 举报
回复
致: taosihai1only(我是菜鸟,我怕谁)
什么意思?
apache8888 2003-11-02
  • 打赏
  • 举报
回复
up
seraph2 2003-11-02
  • 打赏
  • 举报
回复
其他的我就不说了,代码概念如下
dim ll as boolean
for i=0 to ubound(数组名称)
if 数组名称(i)=预定数 then
ll=真
exit for
else
ll= 假
end if
next i
比较完了,不会连排序都让我帮你做吧?
xmzorro 2003-11-02
  • 打赏
  • 举报
回复
致: raisuny(太阳雨)
这位大虾啊,你能不能帮忙写个比较详细的程序?
xmzorro 2003-11-01
  • 打赏
  • 举报
回复
致:FSoft() ( )
因为我最后是要输出这个数组,而老师要求这个数组的值是不能重复。没办法,如果可以重复岂不简单?
taosihai1only 2003-11-01
  • 打赏
  • 举报
回复
借助列表框,代码简单一些
xmzorro 2003-11-01
  • 打赏
  • 举报
回复
刚打了个电话给老师,她告诉我思路了,我自己应该能搞定。写好了就上来分分数!谢谢大家的参与!有好点子还可以写,一样送分!
northwolves 2003-10-31
  • 打赏
  • 举报
回复
这样就可以,借助列表框,代码简单一些:
1。加一个LISTBOX,SORT属性设为TRUE,VISIBLE 属性也可为FALSE
2。将数组全部元素加入LISTBOX
3。判断LISTBOX 中有无TEXT1.TEXT,有则删除之,无则加之
4。循环将LISTBOX中元素输出在TEXT2
Gelim 2003-10-31
  • 打赏
  • 举报
回复
疑问:

1、如果在则删除数组中的这个数,那么数组的大小需不需要减小

2、如果没有则赋值给该数组中的任一数,什么意思?
xmzorro 2003-10-31
  • 打赏
  • 举报
回复
致:danielinbiti(金)
不要光说不练啊,看不懂啊!
xmzorro 2003-10-31
  • 打赏
  • 举报
回复
致:zhouqi66()
这位大虾啊,您能FRM发到我的信箱:xmzorro@163.com
或者将FRM转成TXT贴上来吗?
FSoft 2003-10-31
  • 打赏
  • 举报
回复
不知这样行不行,还有,如果Text1中的内容存在于数组中,为何要删除数组而不选择不添加?

Dim x As Variant, StrX As String, Lngnums(255) As Long, P As Boolean, y As Long
StrX = Text1.Text
For Each x In Lngnums
If x = StrX Then
P = True
Exit For
End If
Next x
If P = False Then
Lngnums(y) = Val(Text1.Text)
y = y + 1
End If
danielinbiti 2003-10-31
  • 打赏
  • 举报
回复
用集合查找也是不错的选择
zhouqi66 2003-10-31
  • 打赏
  • 举报
回复
For i = LBound(arr) To UBound(arr)
If arr(i) >= Text1.Text Then
position = i
Exit For
End If
Next i
If arr(position) = Text1.Text Then
For i = position To unbound(arr) - 1
arr(i) = arr(i + 1)
Next i
Else
ReDim Preserve arr(UBound(arr) + 1)
For i = unbound(arr) To position + 1 Step -1
arr(i) = arr(i + 1)
Next i
arr(position) = Text1.Text
End If
xmzorro 2003-10-31
  • 打赏
  • 举报
回复
致: zhouqingyuan(浪帆)
这个我知道,在查找有无相同元素时排序。但排序后我不知道接下来数组里的哪个值是空的!所以才来线上求援来了!

因为我学习VB没几天,对一些功能、语句还不是很熟悉。对系统提示的错误不知道如何排除。您们提供方法时能否写得详细点?

谢谢!
xmzorro 2003-10-31
  • 打赏
  • 举报
回复
致:libirds(博德)
我不用判断啊,只要LEN判断文字长度就好!比如
xmzorro 2003-10-31
  • 打赏
  • 举报
回复
致:rainstormmaster(rainstormmaster)
您说的方法看不懂啊,能说详细点吗?我建了个EXT文件,但电脑提示变量未定义。这是我用的FRM文件,请帮忙看一下哪里出问题了!谢谢!
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 4875
ClientLeft = 4080
ClientTop = 5400
ClientWidth = 6375
LinkTopic = "Form1"
ScaleHeight = 4875
ScaleWidth = 6375
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 975
Left = 4440
TabIndex = 3
Top = 1320
Width = 1695
End
Begin VB.ListBox List1
Height = 3660
Left = 120
TabIndex = 2
Top = 960
Width = 3015
End
Begin VB.TextBox Text2
Height = 615
Left = 120
TabIndex = 1
Text = "Text2"
Top = 120
Width = 6135
End
Begin VB.TextBox Text1
Height = 1095
Left = 4440
TabIndex = 0
Text = "Text1"
Top = 3480
Width = 1695
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Function Exists(Key) As Boolean
Scripting.Dictionary 的成员
Determine if a given key is in the dictionary

Option Explicit
Dim arr() As Long
Dim dic As New Dictionary
Dim i As Long



Private Sub Command1_Click()
Dim j As Long
j = CLng(Text1.Text)
If dic.Exists(j) = False Then
dic.Add j, CStr(j)
ReDim Preserve arr(i + 1)
arr(i + 1) = j
Debug.Print arr(i + 1)
End If

End Sub

Private Sub Form_Load()
'初始化数组和词典
For i = 0 To 200
ReDim Preserve arr(i)
arr(i) = i
dic.Add i, CStr(i)
Next

End Sub

libirds 2003-10-31
  • 打赏
  • 举报
回复
你在输入数时一定有确定按钮来判断吧,那就用instr先来判断数组是否存在该数。如果有就在数组中删了,没有就添加啊。
rainstormmaster 2003-10-31
  • 打赏
  • 举报
回复
Option Explicit
Dim arr() As Long
Dim dic As New Dictionary
Dim i As Long
Private Sub Command1_Click()
Dim j As Long
j = CLng(Text1.Text)
If dic.Exists(j) = False Then
dic.Add j, CStr(j)
ReDim Preserve arr(i + 1)
arr(i + 1) = j
Debug.Print arr(i + 1)
End If

End Sub

Private Sub Form_Load()

'初始化数组和词典
For i = 0 To 200
ReDim Preserve arr(i)
arr(i) = i
dic.Add i, CStr(i)
Next
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set dic = Nothing
End Sub
加载更多回复(5)

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧