none
如何篇写我的string那些可以被3 除或 5 除 RRS feed

  • 问题

  • Array = [1,33,99,199,25]

    我想问一下如何编写代码来排列我的数组号可以除以3和5
    谢谢。

    比如:33和99 可以被3 除

    比如:25 可以被5 除


    2019年12月23日 9:39

答案

  • Hi christing, 

    你可以通过取余’%’来找出数组中可以被3或被5整除的所有数。

    代码:

            Dim array = {1, 33, 99, 199, 25, 72, 77, 10, 120}
            Dim canDev3 = New List(Of Integer)
            Dim canDev5 = New List(Of Integer)
    
            For Each number In array
    
                If number Mod 3 = 0 Then
                    canDev3.Add(number)
                ElseIf number Mod 5 = 0 Then
                    canDev5.Add(number)
                End If
            Next
    
            Console.Write("numbers which can devide three: ")
    
            For Each number3 In canDev3
                Console.Write($"{number3} ")
            Next
    
            Console.WriteLine()
            Console.Write("numbers which can devide five:: ")
    
            For Each number5 In canDev5
                Console.Write($"{number5} ")
            Next
    
            Console.ReadLine()
    

    测试结果:

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年12月24日 1:49
    版主