表格输出怎么编程

时间:2025-01-24 20:24:04 网络游戏

在不同的编程语言中,输出表格的方法有所不同。以下是一些常见编程语言中输出表格的示例代码:

C语言

```c

include

define TABLE_ROWS 3

define TABLE_COLS 3

int main() {

int data[TABLE_ROWS][TABLE_COLS] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

// 输出表头

printf("Column 1 Column 2 Column 3\n");

printf("-" TABLE_COLS * 10 + "\n");

// 输出表格内容

for (int i = 0; i < TABLE_ROWS; i++) {

printf("Row %d", i + 1);

for (int j = 0; j < TABLE_COLS; j++) {

printf("%9d", data[i][j]);

}

printf("\n");

}

return 0;

}

```

Java

```java

public class Main {

static int size = 12;

static void printTable(String name, String course, String score) {

String[] table = {name, course, score};

for (int i = 0; i < 3; ++i) {

System.out.print("|");

int len = table[i].length();

int leftSpace = (size - len) / 2;

int rightSpace = size - len - leftSpace;

for (int j = 0; j < leftSpace; ++j) {

System.out.print(" ");

}

for (int j = 0; j < len; ++j) {

System.out.print(table[i].charAt(j));

}

for (int j = 0; j < rightSpace; ++j) {

System.out.print(" ");

}

System.out.println("|");

}

}

public static void main(String[] args) {

printTable("Name", "Course", "Score");

}

}

```

Python

```python

def print_table(data):

print("{:<12} {:<12} {:<12}".format(*data))

for row in data[1:]:

print("{:<12} {:<12} {:<12}".format(*row))

data = [

["Name", "Course", "Score"],

["Alice", "Math", "95"],

["Bob", "Math", "88"],

["Charlie", "Math", "92"]

]

print_table(data)

```

Excel VBA

```vba

Sub DoubleCellValue()

Dim selectedCell As Range

Set selectedCell = Selection

If Not selectedCell Is Nothing Then

selectedCell.Value = selectedCell.Value * 2

End If

End Sub

```

这些示例代码展示了如何在不同的编程语言中输出表格。你可以根据自己的需求选择合适的编程语言和输出方法。