0 / 8 页已完成

变量是什么What Are Variables?

3.1 变量就像一个盒子3.1 A Variable Is Like a Box

想象你有一个盒子,盒子上贴了一个标签写着"年龄"。你可以往盒子里放一个数字,比如 14。Imagine you have a box with a label that says "age." You can put a number in it, like 14.

以后你想知道年龄是多少,就看看盒子里的东西。你也可以把 14 拿出来,换成 15 -- 盒子还是那个盒子,标签没变,但里面的东西变了。Later, when you want to know the age, just look inside the box. You can also take out 14 and replace it with 15 — the box is the same, the label hasn't changed, but the content inside has.

变量 = 带标签的盒子Variable = A Labeled Box

标签是变量的名字,盒子里的东西是变量的。程序运行时,值可以变,名字不变。The label is the variable's name, and the content inside is the variable's value. While the program runs, the value can change, but the name stays the same.

3.2 声明一个变量3.2 Declaring a Variable

在 C++ 里,创建(声明)一个变量需要三样东西:类型名字初始值In C++, creating (declaring) a variable requires three things: a type, a name, and an initial value.

C++
int age = 14;          // 整数:年龄是 14
double speed = 3.5;    // 小数:速度是 3.5
const char* name = "VEX";  // 文字:名字是 VEX

分解来看:Breaking it down:

3.3 三种常用类型3.3 Three Common Types

类型Type 用来存什么What It Stores 例子Example
int 整数(没有小数点)Whole numbers (no decimal point) int score = 100;
double 小数(有小数点)Decimal numbers (with decimal point) double voltage = 12.6;
const char* 文字(字符串)Text (string) const char* team = "12345A";
为什么文字类型写 const char* 而不是 string?Why use const char* instead of string?

在 VEX 的 C++ 环境里,文字通常用 const char*。你现在不需要理解为什么,只要记住:存文字就用 const char*In VEX's C++ environment, text is usually stored with const char*. You don't need to understand why right now — just remember: use const char* for text.

3.4 变量可以改3.4 Variables Can Change

变量之所以叫"变量",就是因为它的值可以变Variables are called "variables" because their values can change.

C++
int age = 14;    // 一开始是 14
age = 15;        // 现在变成 15 了
age = age + 1;   // 在当前值的基础上加 1,变成 16

注意:改变量的值时,不用再写类型(不用再写 int)。类型只在第一次声明时写一次。Note: When changing a variable's value, you don't write the type again (no need to write int again). The type is only written once when you first declare it.

3.5 把变量显示在屏幕上3.5 Display Variables on the Screen

我们可以把变量的值打印到 Brain 屏幕上:We can print a variable's value to the Brain screen:

C++ (VEXcode)
int main() {
    int score = 88;
    Brain.Screen.print("Score: %d", score);
    return 0;
}

%d 是一个占位符,运行时会被 score 的值(88)替换掉。屏幕显示:Score: 88%d is a placeholder that gets replaced by the value of score (88) at runtime. The screen shows: Score: 88

C++ (VS Code)
int main() {
    int score = 88;
    Brain.Screen.print("Score: %d", score);
    return 0;
}

%d 是一个占位符,运行时会被 score 的值(88)替换掉。屏幕显示:Score: 88%d is a placeholder that gets replaced by the value of score (88) at runtime. The screen shows: Score: 88

C++ (PROS)
void initialize() {
    pros::lcd::initialize();
    int score = 88;
    pros::lcd::print(1, "Score: %d", score);
}

%d 是一个占位符,运行时会被 score 的值(88)替换掉。屏幕第 1 行显示:Score: 88%d is a placeholder that gets replaced by the value of score (88) at runtime. Line 1 of the screen shows: Score: 88

常用占位符Common Placeholders
检查点:预测输出Checkpoint: Predict the Output
这段代码运行后,x 的值是多少?After running this code, what is the value of x?
C++
int x = 5;
x = x + 3;

3.6 写代码练习3.6 Coding Exercise

挑战Challenge

写一段代码:Write a program that:

  1. 创建一个 int 变量 laps,初始值为 0Creates an int variable laps with initial value 0
  2. laps 增加 1(模拟跑了一圈)Increases laps by 1 (simulating one lap)
  3. 再增加 1(又跑了一圈)Increases it by 1 again (another lap)
  4. laps 的值显示到屏幕上Displays the value of laps on the screen

屏幕应该显示什么数字?What number should the screen show?

检查点Checkpoint
下面哪行代码能正确地把变量 speed 的值从 3.0 改成 5.0?(假设已经声明过 double speed = 3.0;Which line of code correctly changes the variable speed from 3.0 to 5.0? (Assume double speed = 3.0; has already been declared)