变量是什么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.
标签是变量的名字,盒子里的东西是变量的值。程序运行时,值可以变,名字不变。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.
int age = 14; // 整数:年龄是 14
double speed = 3.5; // 小数:速度是 3.5
const char* name = "VEX"; // 文字:名字是 VEX
分解来看:Breaking it down:
int— 类型,表示这个盒子只能放整数int— The type, meaning this box can only hold whole numbersage— 名字,你给这个盒子贴的标签age— The name, the label you put on the box= 14— 初始值,一开始放进去的东西= 14— The initial value, what you put inside at the start;— 分号,每行代码的结尾都要加;— Semicolon, required at the end of every line of code
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"; |
在 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.
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:
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
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
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
%d— 整数(int)%d— Whole number (int)%f— 小数(double)%f— Decimal number (double)%s— 文字(const char*)%s— Text (const char*)
x 的值是多少?After running this code, what is the value of x?
int x = 5;
x = x + 3;
3.6 写代码练习3.6 Coding Exercise
写一段代码:Write a program that:
- 创建一个
int变量laps,初始值为 0Creates anintvariablelapswith initial value 0 - 让
laps增加 1(模拟跑了一圈)Increaseslapsby 1 (simulating one lap) - 再增加 1(又跑了一圈)Increases it by 1 again (another lap)
- 把
laps的值显示到屏幕上Displays the value oflapson the screen
屏幕应该显示什么数字?What number should the screen show?
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)