陣列處理

畫面

主程式

Imports System.Text Public Class FormDS000 Private TempArray() As Integer Private Sub ToolStripButtonQuit_Click(sender As Object, e As EventArgs) Handles ToolStripButtonQuit.Click Me.Close() End Sub Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. Me.ListBoxResult.Items.Clear() End Sub Private Sub ToolStripButtonGetSum_Click(sender As Object, e As EventArgs) Handles ToolStripButtonGetSum.Click If Integer.Parse(Me.TextBoxMaxNumOfArray.Text) > 0 Then If Integer.Parse(Me.TextBoxNumOfMod.Text) > 0 Then Dim TempArray(Integer.Parse(Me.TextBoxMaxNumOfArray.Text)) As Integer Dim j As Integer = 0 For i = 1 To UBound(TempArray) + 1 If i Mod Integer.Parse(Me.TextBoxNumOfMod.Text) = 0 Then TempArray(j) = i j = j + 1 Else TempArray(j) = 0 End If Next Me.ListBoxResult.Items.Clear() Me.ListBoxResult.Items.Add("個數 = " & j.ToString) Me.ListBoxResult.Items.Add(" 和 = " & TempArray.Sum.ToString) Me.ListBoxResult.Items.Add("") Dim TempBuffer As StringBuilder = New StringBuilder() For i = 0 To j - 1 TempBuffer.Append(TempArray(i).ToString.PadLeft(Math.Floor(1 + Math.Log10(Integer.Parse(Me.TextBoxMaxNumOfArray.Text))))) If (i + 1) Mod 10 = 0 Then Me.ListBoxResult.Items.Add(TempBuffer.ToString) TempBuffer.Clear() Else TempBuffer.Append(vbTab) End If Next Me.ListBoxResult.Items.Add(TempBuffer.ToString) End If End If End Sub Private Sub ToolStripButtonSwap_Click(sender As Object, e As EventArgs) Handles ToolStripButtonSwap.Click Dim TempArray() As String = Nothing If Not Me.TextBoxArray.Text.Trim() = "" Then TempArray = Me.TextBoxArray.Text.Trim().Split(",") Dim NumArray(UBound(TempArray)) As Integer Dim j As Integer = 0 For i = 0 To UBound(TempArray) Try NumArray(j) = Integer.Parse(TempArray(i)) j = j + 1 Catch ex As Exception End Try Next Dim TempValue As Integer = 0 For i = 0 To Math.Floor(UBound(NumArray) / 2) TempValue = NumArray(i) NumArray(i) = NumArray(UBound(NumArray) - i) NumArray(UBound(NumArray) - i) = TempValue Next Me.ListBoxResult.Items.Clear() Dim TempBuffer As StringBuilder = New StringBuilder() For i = 0 To UBound(NumArray) TempBuffer.Append(NumArray(i).ToString) If (i + 1) Mod 10 = 0 Then Me.ListBoxResult.Items.Add(TempBuffer.ToString) TempBuffer.Clear() Else TempBuffer.Append(vbTab) End If Next Me.ListBoxResult.Items.Add(TempBuffer.ToString) End If End Sub End Class