第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何根據(jù)表中的數(shù)據(jù)增加或減少列數(shù)?

如何根據(jù)表中的數(shù)據(jù)增加或減少列數(shù)?

Go
holdtom 2023-02-14 18:23:37
這是在表中打印數(shù)據(jù)的代碼,每列包含多個(gè)數(shù)據(jù)。這樣一來(lái),每一列就很難打印出準(zhǔn)確的數(shù)據(jù)個(gè)數(shù)。這就是為什么如果數(shù)據(jù)的數(shù)量是三并且循環(huán)的限制是 2 那么它不會(huì)打印最后一個(gè)數(shù)據(jù)列并且循環(huán)將在 2 處停止。如何根據(jù)數(shù)據(jù)調(diào)整列?要求的結(jié)果╔═══╤════════════════╤═════════════════════╗║ # │    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 回答

?
達(dá)令說(shuō)

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊

有一系列可能的方法;這是一個(gè)選項(xiàng),無(wú)需對(duì)行數(shù)/列數(shù)做出假設(shè)(操場(chǎng)):


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()

}


查看完整回答
反對(duì) 回復(fù) 2023-02-14
  • 1 回答
  • 0 關(guān)注
  • 119 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)