2015-07-12 72 views
2

假設開始是1並且停止是10.我想在列號中打印1到5,然後在另一列中打印6-10。如何打印拆分列?

我可以在單循環中做到這一點嗎?或者我必須做另一個循環? 我也無法弄清楚如何在第二列其中{1, -10}

Console.WriteLine("{0,-10}", "Number") 

    Dim split = (starting + stopping)/2 

    For A = starting To split 
     Console.WriteLine("{0,-10}", A) 
    Next 

UPDATE開始 - 預期的結果

Number Number 
1  6 
2  7 
3  8 
4  9 
5  10 

更新2 - 使它們對齊 而不是使用空間()任何其他解決方案,我們可以使它們的寬度相同,並將其排列在中間列 enter image description here

+0

'vba'或'vb.net'?這些是不同的框架。你能展示你期望的結果嗎? – Fabio

+0

已更新的問題@Fabio – vzhen

+0

使用'vbTab'常量:'Console.WriteLine(「{0} {1} {2}」,column1Value,vbTab,column2Value)' – Fabio

回答

1

我會寫這樣的事情,使用一個循環:

Sub TwoColumns(ByVal starting as Integer,ByVal stopping as Integer) 
    Dim split As Integer,i As Integer 
    split = (starting + stopping)/2 
    For i=starting to split 
    Debug.Print (i) & Space(5) & (i+split-starting) 
    Next i 
End Sub 
+0

謝謝,但還有1個問題。我們可以使用Space()嗎? ,因爲它們不以相同的寬度打印。檢查我的更新圖片。 – vzhen

+0

並將它們排列在中間列 – vzhen

+0

我想通了。謝謝 – vzhen

0

當製作琴絃上的控制檯輸出對齊,我通常使用功能與空間填充(無論是向左或向右),當你想非常有用格式化輸出。

下面將適合任意數量的列來拆分您擁有的數組(更一般,因爲您可以設置要拆分的列數)。如果您只是想以表格的形式顯示數組,請使用TabulateArray(...)

Sub DisplayTuplesTest() 
    Dim arr1 As Variant, arr2 As Variant 
    arr1 = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) 
    arr2 = Array(1.61, 3.22, 4.83, 6.44, 8.05, 9.66, 11.27, 12.88, 14.49, 16.1, 17.71, 19.32, 20.93, 22.54, 24.15, 25.76, 27.37, 28.98, 30.59, 32.2) 
    Call DisplayTuples(arr1, arr2, 2) 
    Call TabulateArray(arr2, 2) 
End Sub 

Sub DisplayTuples(Array1 As Variant, Array2 As Variant, iCols As Integer) 
    Dim Count1 As Integer, Count2 As Integer ' Counts # of items in the array 
    Dim Max1 As Integer, Max2 As Integer ' Tracks the max length of values 
    Dim n As Integer, oTmp As Variant ' Temporary variables 
    Dim r As Integer, c As Integer, rows As Integer, sTxt As String ' For displaying results 

    Count1 = UBound(Array1) 
    Count2 = UBound(Array2) 
    If Count1 <> Count2 Then 
     Debug.Print "Arrays are not same dimension!" 
     Exit Sub 
    End If 

    ' Max length of Array1 
    Max1 = 0 
    For Each oTmp In Array1 
     n = Len(CStr(oTmp)) 
     If Max1 < n Then Max1 = n 
    Next 

    ' Max length of Array2 
    Max2 = 0 
    For Each oTmp In Array2 
     n = Len(CStr(oTmp)) 
     If Max2 < n Then Max2 = n 
    Next 

    ' Lets say we want 4 spaces from longest value of Array1 value before showing Array2 value 
    Max1 = Max1 + 4 
    ' Lets say we want 8 spaces from longest value of Array2 value before showing Array1 value on next column 
    Max2 = Max2 + 8 

    ' Rows required to display arrays into iCols columns 
    rows = Count1 \ iCols 
    If rows * iCols < Count1 Then rows = rows + 1 ' If an extra rows is required 

    ' Display in a table like format, with spliting into iCols columns 
    For r = 0 To rows - 1 
     sTxt = "" 
     For c = 0 To iCols - 1 
      n = r + c * rows ' calculates the n'th index to display 
      If n > Count1 Then Exit For 
      sTxt = sTxt & SpacePadding_R(Array1(n), Max1) & SpacePadding_R(Array2(n), Max2) 
     Next 
     Debug.Print sTxt 
    Next 
End Sub 

Sub TabulateArray(Array1 As Variant, iCols As Integer) 
    Dim Count1 As Integer ' Counts # of items in the array 
    Dim Max1 As Integer, Max2 As Integer ' Tracks the max length of values 
    Dim n As Integer, oTmp As Variant ' Temporary variables 
    Dim r As Integer, c As Integer, rows As Integer, sTxt As String ' For displaying results 

    Count1 = UBound(Array1) 

    ' Max length of n'th index 
    Max1 = Len(CStr(Count1 + 1)) 

    ' Lets say we want 3 spaces from max count of Array1 value before showing the Array1 value 
    Max1 = Max1 + 4 
    ' Lets say we want 8 spaces from longest value of Array1 value before next column 
    Max2 = Max2 + 8 

    ' Rows required to display arrays into iCols columns 
    rows = Count1 \ iCols 
    If rows * iCols < Count1 Then rows = rows + 1 ' If an extra rows is required 

    ' Display in a table like format, with spliting into iCols columns 
    For r = 0 To rows - 1 
     sTxt = "" 
     For c = 0 To iCols - 1 
      n = r + c * rows ' calculates the n'th index to display 
      If n > Count1 Then Exit For 
      sTxt = sTxt & SpacePadding_R(n + 1, Max1) & SpacePadding_R(Array1(n), Max2) 
     Next 
     Debug.Print sTxt 
    Next 
End Sub 

Function SpacePadding_R(oValue As Variant, iMaxLen As Integer) As String 
    Dim sOutput As String, iLen As Integer 
    sOutput = CStr(oValue) 
    iLen = Len(sOutput) 
    If iLen < iMaxLen Then 
     ' Pad spaces at the end of the input (Right side) 
     sOutput = sOutput & String(iMaxLen - iLen, " ") 
    Else 
     ' Optional if you want to restrict this 
     sOutput = Left(sOutput, iMaxLen) 
    End If 
    SpacePadding_R = sOutput 
End Function 

輸出與樣本陣列(高亮向您顯示添加空格):
Sample Output