计数方向做循环 [英] Count direction do loop
本文介绍了计数方向做循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
2个文本框
2个列表框
做while循环
我想在列表框中更改结果的方向,以根据文本框的输入进行更改.因此,如果Txtbox 1 = 0 Txtbox 2 = 10,则Listbox 1 = 1,3,5,7,9 Listbox 2-2,4,6,8,10;如果Txtbox 1 = 10 Txtbox 2 = 0,则Listbox 1 = 9 ,7,5,3,1列表框2 = 10,8,6,4,2
我要做什么
2 Textboxs
2 list boxs
do while loop
I want to change the direction of the result in the Listbox to change based on the input of the textbox. So if Txtbox 1 = 0 Txtbox 2 = 10 then Listbox 1 = 1,3,5,7,9 Listbox 2 - 2,4,6,8,10 and if Txtbox 1 = 10 Txtbox 2 = 0 then Listbox 1 = 9,7,5,3,1 Listbox 2 = 10,8,6,4,2
What i am trying to do
LowerB = CType(txtLB.Text, Integer)
UpperB = CType(txtUB.Text, Integer)
If LowerB < UpperB Then
NumCounterI = CountUp
Else
NumCounterI = Countdown
End If
If LowerB < UpperB Then
NumCounterI = LowerB
Do While NumCounterI <= UpperB
If NumCounterI <> 0 Then
If NumCounterI Mod 2 = EvenNumber Then
lstEvens.Items.Add(NumCounterI)
Else
lstOdds.Items.Add(NumCounterI)
End If
End If
NumCounterI += 1
Loop
推荐答案
一个简单的解决方案:
A Simple Solution:
If LowerB < UpperB Then
IncrementStep = 1
Else
IncrementStep = -1
End If
For NumCounterI As Integer = LowerB To UpperB Step IncrementStep
If NumCounterI Mod 2 = 0 Then
ListBox1.Items.Add(NumCounterI.ToString())
Else
ListBox2.Items.Add(NumCounterI.ToString())
End If
Next
rgds
rgds
我认为LINQ可以按如下方式使用来生成序列
I think LINQ can be used as follows to generate the sequences
Dim lb As Integer = 10
Dim ub As Integer = 0
Dim seq = Enumerable.Range(Math.Min(lb, ub), Math.Max(lb, ub) / 2)
Dim seq1 = If(lb > ub, seq.[Select](Function(s) s * 2 + 1).Reverse(), _
seq.[Select](Function(s) s * 2 + 1))
Dim seq2 = If(lb > ub, seq.[Select](Function(s) s * 2 + 2).Reverse(), _
seq.[Select](Function(s) s * 2 + 2))
根据OP [/Edit]的注释添加了使用do while loop
的替代方法
Alternative using do while loop
added as per the comment of OP [/Edit]
Dim lb = 10
Dim ub = 0
Dim list1 as new List(Of integer)
Dim list2 as new List(Of integer)
Dim inc = IF(lb < ub, 2, -2)
Dim seed1 = IF(lb < ub, 1, 9)
Dim seed2 = IF(lb < ub, 2, 10)
Dim ctr = 0
do while ctr < 5
list1.Add(seed1 + ctr * inc)
list2.Add(seed2 + ctr * inc)
ctr += 1
loop
这篇关于计数方向做循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文