如何在vb.net中循环多个条件

时间:2017-03-14 09:28:27

标签: vb.net loops for-loop

我可以循环2条件vb net吗?

Dim s As New DirectoryInfo("C:/ProgramFiles")
Dim files As FileInfo() = s.GetFiles("*.jpg")
For i As Integer = 1 To files.Count 'condition 1 get loop Number of Files
    For Each f As FileInfo In files 'condition 2 get loop Files
    Dim bmp As New Bitmap(f.FullName)
    If bmp.Width >= 1000 Then
        Console.WriteLine(i & ". True--> Name : " & f.FullName & " Width: " & bmp.Width.ToString() + " > Height: " & bmp.Height.ToString())
    Else
        Console.WriteLine(i & ". False--> Name : " & f.FullName & " Width: " & bmp.Width.ToString() + " > Height: " & bmp.Height.ToString())
    End If
    Next
Next
Console.Read()

如何获得每个结果中的数字?

示例编号:1,2,3,...

1. True--> Name : a.jpg Width:1000 > Height:500
2. False--> Name : b.jpg Width:800 > Height:400
3. True--> Name : c.jpg Width:1200 > Height:600
4... 

但代码结果是:

1. True--> Name : a.jpg Width:1000 > Height:500
1. False--> Name : b.jpg Width:800 > Height:400
1. True--> Name : c.jpg Width:1200 > Height:600
2. True--> Name : a.jpg Width:1000 > Height:500
2. False--> Name : b.jpg Width:800 > Height:400
2. True--> Name : c.jpg Width:1200 > Height:600 
3...

我是vb.net的初学者编码

感谢您的建议,

2 个答案:

答案 0 :(得分:0)

/ *简单* /

Dim Count As Integer = 1;
I循环结束时用count ++增加它;

并将i替换为此变量,以便您可以获得正确的索引

答案 1 :(得分:0)

移除外环,你不需要它。为了输出一个索引,只需使用一个变量并在循环中递增它:

Dim s As New DirectoryInfo("C:/ProgramFiles")
Dim files As FileInfo() = s.GetFiles("*.jpg")
Dim count As Integer = 1
For Each f As FileInfo In files 
    Dim bmp As New Bitmap(f.FullName)
    If bmp.Width >= 1000 Then
         Console.WriteLine(count & ". True--> Name : " & f.FullName & " Width: " & bmp.Width.ToString() + " > Height: " & bmp.Height.ToString())
   Else
        Console.WriteLine(count & ". False--> Name : " & f.FullName & " Width: " & bmp.Width.ToString() + " > Height: " & bmp.Height.ToString())
   End If
   count += 1
Next

Console.Read()