安慰#

介绍#

VEXcode IQ(第二代)控制台显示在 VEXcode 的监视器窗口中。项目运行时,控制台输出可以显示文本、数字和变量值。

使用 VS Code 时,基于文本的控制台输出会出现在终端中,而不是 VEXcode 控制台中。

注意: 使用 VEXcode 控制台时,必须先连接到控制台串口,然后才能将任何输出打印到控制台。

以下是所有功能的列表:

操作 — 输出文本。

  • printf — Displays text, numbers, or variable values in the Console or terminal.

行动#

printf#

printf displays text, numbers, or variable values in the Console. In VS Code, printf displays output in the terminal.

In C++, printf uses a format string. The format string is the text inside the quotation marks. It can include regular text, control sequences, and format specifiers that insert values from your project, like a score, timer, or sensor reading. See the String Formatting page for more information about format specifiers.

By default, printf does not automatically move to the next line. Add \n, which stands for a new line, when you want the next printed value to start on a new row.

Usage:
printf(value);

范围

类型

描述

value

const char*

要在控制台或终端中显示的文本、格式字符串或控制序列。

返回值:
此函数不返回值。

// Display a message in the Console
printf("Hello, robot!\n");

VEXcode 控制台,显示控制台和文本“Hello, robot!”。

// Display two messages on separate lines
printf("Hello, robot!\n");
printf("This prints on the next line.\n");

VEXcode 控制台,显示控制台和文本“Hello, robot!”(显示在第一行)以及“This prints on the next line”(显示在其下一行)。

// Display two messages on the same line
printf("Hello, ");
printf("robot!\n");

VEXcode 控制台,显示控制台和文本“Hello, robot!”。

// Display a number with two decimal places
printf("%.2f\n", 1.0 / 3.0);

VEXcode 控制台,显示控制台和文本“0.33”。

控制序列#

Control sequences are short codes that can be printed with printf to change how text appears in the Console.

Control sequences are useful when you want to change text color or clear the Console directly inside a printf statement.

To change the text color, print \033 followed by a color code. At the start of a project, the Console text color is set to blue.

颜色

控制序列

黑色的

\033[30m

红色的

\033[31m

绿色的

\033[32m

黄色的

\033[33m

蓝色的

\033[34m

紫色的

\033[35m

青色

\033[36m

白色的

\033[37m

橙子

\033[91m

// Print text in different colors
printf("Default text color\n");
printf("\033[31m");
printf("Red text color\n");

VEXcode 控制台,显示控制台和蓝色文字“默认文本颜色”(第一行),红色文字“红色文本颜色”(第二行)。

清除控制台#

C++ does not have a separate Console function for clearing all rows. To clear the Console, print the control sequence \033[2J with printf.

这将清除控制台中的所有行,并将光标移回第 1 行。

// Clear the Console after two seconds
printf("VEXcode\n");
wait(2, seconds);
printf("\033[2J");