且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使comboBox中的项目值具有特定范围?

更新时间:2023-01-17 17:53:03

您错过了条件部分.

尝试这样的事情.

You miss the condition part.

Try something like this.

int i = 0;
var random = new Random();
var randomList = new List<int>();

while (i < 5) {
   int x = random.Next(1, 15);
   if (!randomList.Contains(x) || !randomList.Contains((x + 1)) || !randomList.Contains((x - 1))) {
      randomList.Add(i);
      ++i;
   }
}

randomList.ForEach(r => cmbRnd.Items.Add(r.ToString()));




VB




VB

Dim i As Integer = 0
Dim random = New Random()
Dim randomList = New List(Of Integer)()

While i < 5
    Dim x As Integer = random.[Next](1, 15)

    If Not randomList.Contains((x + 1)) OrElse Not randomList.Contains((x - 1)) OrElse Not randomList.Contains(x) Then
        randomList.Add(i)
        i += 1
    End If
End While

randomList.ForEach(Function(r) cmbRnd.Items.Add(r.ToString()))



希望对您的任务有所帮助.

< edit>
重要的是条件
列表应该包含(随机,随机+1或随机-1)



Hope it helps for your assignment.

<edit>
The important part is the condition
List should not contain (random or random+1 or random-1)


您要做的就是增加测试范围:
All you have to do is increase the range of tests:
If Not cmbRnd.Items.Contains(newItem) Then

成为

If Not (cmbRnd.Items.Contains(newItem - 1) OrElse cmbRnd.Items.Contains(newItem) OrElse cmbRnd.Items.Contains(newItem + 1) Then