亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何根據表中的數據增加或減少列數?

如何根據表中的數據增加或減少列數?

Go
holdtom 2023-02-14 18:23:37
這是在表中打印數據的代碼,每列包含多個數據。這樣一來,每一列就很難打印出準確的數據個數。這就是為什么如果數據的數量是三并且循環的限制是 2 那么它不會打印最后一個數據列并且循環將在 2 處停止。如何根據數據調整列?要求的結果╔═══╤════════════════╤═════════════════════╗║ # │    Projects    │ Project Priorities  ║╟━━━┼━━━━━━━━━━━━━━━━┼━━━━━━━━━━━━━━━━━━━━━╢║ 1 │ first project  │       None          ║║ 2 │ second project │       Low           ║║ 3 │                │      Medium         ║║ 4 │                │       High          ║╚═══╧════════════════╧═════════════════════╝代碼package mainimport (    "fmt"    "github.com/alexeyco/simpletable")func InfoTable(allData [][]string) {    table := simpletable.New()    table.Header = &simpletable.Header{        Cells: []*simpletable.Cell{            {Align: simpletable.AlignCenter, Text: "#"},            {Align: simpletable.AlignCenter, Text: "Projects"},            {Align: simpletable.AlignCenter, Text: "Project Priorities"},        },    }    var cells [][]*simpletable.Cell    for i := 0; i < 2; i++ {        cells = append(cells, *&[]*simpletable.Cell{            {Align: simpletable.AlignCenter, Text: fmt.Sprintf("%d", i+1)},            {Align: simpletable.AlignCenter, Text: allData[0][i]},            {Align: simpletable.AlignCenter, Text: allData[1][i]},        })    }    table.Body = &simpletable.Body{Cells: cells}    table.SetStyle(simpletable.StyleUnicode)    table.Println()}func main() {    data := [][]string{        {"first project", "second project"},        {"None", "Low", "Medium", "High"},    }    InfoTable(data)}
查看完整描述

1 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

有一系列可能的方法;這是一個選項,無需對行數/列數做出假設(操場):


func InfoTable(headings []string, allData [][]string) {

    if len(headings) != len(allData) {

        panic("Must have a heading per column")

    }

    table := simpletable.New()


    // Populate headings (adding one for the row number)

    headerCells := make([]*simpletable.Cell, len(headings)+1)

    headerCells[0] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: "#"}

    for i := range headings {

        headerCells[i+1] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: headings[i]}

    }


    table.Header = &simpletable.Header{

        Cells: headerCells,

    }


    // Work out number of rows needed

    noOfCols := len(allData)

    noOfRows := 0

    for _, col := range allData {

        if len(col) > noOfRows {

            noOfRows = len(col)

        }

    }


    // Populate cells (adding row number)

    cells := make([][]*simpletable.Cell, noOfRows)

    for rowNo := range cells {

        row := make([]*simpletable.Cell, noOfCols+1) // add column for row number

        row[0] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: fmt.Sprintf("%d", rowNo+1)}


        for col := 0; col < noOfCols; col++ {

            if len(allData[col]) > rowNo {

                row[col+1] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: allData[col][rowNo]}

            } else {

                row[col+1] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: ""} // Blank cell

            }

            cells[rowNo] = row

        }

    }

    table.Body = &simpletable.Body{Cells: cells}


    table.SetStyle(simpletable.StyleUnicode)

    table.Println()

}


查看完整回答
反對 回復 2023-02-14
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號