C++ 初级赋值运算符
C++ Beginner Assignment Operators
我是 C++ 的初学者,我正在做这个赋值运算符的示例代码。我不知道我在这里做错了什么。
#include <iostream>
using namespace std;
int main()
{
int x = 200; int y = 100;
x += y; cout << x << endl; //x += y; // x = x + y, x = 200 + 100, output 300.
x -= y; cout << x << endl; //x -= y; // x = x - y, x = 200 - 100, output 100.
x /= y; cout << x << endl; //x /= y; // x = x / y, x = 200 / 100, output 2.
x %= y; cout << x << endl; //x % y; // x = % y, x = 200 % 100, output 0.
return 0;
}
我正在获取
的结果
x -= y = 200
x /= y = 2
x %= y = 2
当它应该是
x -= y = 100
x /= y = 2
x %= y = 0
它实际上是将之前的结果相加。如何阻止代码将结果添加到下一行?谢谢!
每次你像x += y;
一样做赋值时,它会改变x的值。
x += y;
等同于
x = x + y;
表示赋值。 x 的值正在变化。现在是 300。后面的作业也发生了同样的事情。
注意:如果你不想改变x和y的值,最好在其他变量中赋值。
int x = 200, y = 100, t;
t = x + y; cout << t << endl;
t = x - y; cout << t << endl;
t = x * y; cout << t << endl;
t = (int) x / y; cout << t << endl;
当x
和y
是变量,U
是一个操作时x U= y
等价于:
x = x U y;
这意味着原始变量正在被修改并赋值给结果。
您的代码相当于:
x = x + y;
cout << x << endl; // x is now 300
x = x - y;
cout << x << endl; // x is now 200
x = x / y;
cout << x << endl; // x is now 2
x = x % y;
cout << x << endl; // x is now 2
如果您不想更改 x
,您可以将 x
保存在临时变量中或只打印结果:
int x = 200;
int y = 100;
cout << x - y << endl; // x unchanged
cout << x + y << endl; // x unchanged
cout << x / y << endl; // x unchanged
cout << x * y << endl; // x unchanged
或:
int x = 200;
int x = 100;
int result = x + y;
cout << result << endl;
result = x - y;
cout << x << endl;
result = x / y;
cout << result << endl;
result = x % y;
cout << result << endl;
注:由于=
运算符returns赋值后的值cout << x += y << end
可以编译打印赋值。
正如您在代码中的注释中提到的,x += y
的结果等同于 x = x + y
,这意味着 x
的值将发生变化。因此,x
的新值 300 用于下一个数学作业也就不足为奇了。
如果您想避免这种情况,请考虑将 x + y
的结果保存到另一个变量。
int x = 200;
int y = 100;
int x_addition = x + y;
std::cout << x_addition << std::endl;
或者,如果它的唯一用途是显示加法的结果,则在一行中完成。
int x = 200;
int y = 100;
std::cout << x + y << std::endl;
您没有指望加法运算值
x=200,y=100;
x+=y;//x=200+100=300,x=300,y=100
x-=y;//x=300-100=200,x=200,y=100
这里你忘记了在减法之前有加法操作
尝试在每次分配后将 x 重置回 200。像这样:
#include <iostream>
using namespace std;
int main()
{
int x = 200; int y = 100;
x += y; cout << x << endl;
x = 200;
x /= y; cout << x << endl;
x = 200;
x %= y; cout << x << endl;
return 0;
}
您希望 x
和 y
的原始值永远保留,但事实并非如此。 x += y;
或 x = x + y;
之类的赋值会更改变量的值(在本例中为 x
,即等号左侧的值)。先前的值被覆盖并因此丢失。
如果您不想更改原始值,则必须将结果分配给其他变量(例如,一个名为 temp
,每次都可以覆盖),或者您不' 完全分配它,您只需将计算结果发送到 cout
。
以临时变量为例:
int x = 200; int y = 100; int temp;
temp = x + y; cout << temp << endl; //temp = x + y, temp = 200 + 100, output 300.
temp = x - y; cout << temp << endl; //temp = x - y, temp = 200 - 100, output 100.
或者,完全取消变量:
cout << x + y << endl; // 200 + 100, output 300
cout << x - y << endl; // 200 - 100, output 100
另一种查看变量被覆盖的方法(好吧,在这种情况下它只是 x
)是将它们声明为 const
:
const int x = 200; const int y = 100;
该程序无法编译,因为它试图更改常量变量。
您正在使用累加运算符,因此您正在更改 'x' 的值。
x += y; // x = 200 + 100 = 300
x -= y; // x = 300 - 100 = 200
x /= y; // x = 200 / 100 = 2
x %= y; // x = 2 % 100 = 2
相反你可以这样做:
int x = 200;
int y = 100;
cout << x + y << endl;
cout << x - y << endl;
cout << x / y << endl;
cout << x % y << endl;
或者:
int x = 200;
int y = 100;
int result;
result = x + y;
cout << result << endl;
result = x - y;
cout << result << endl;
result = x / y;
cout << result << endl;
result = x % y;
cout << result << endl;
我是 C++ 的初学者,我正在做这个赋值运算符的示例代码。我不知道我在这里做错了什么。
#include <iostream>
using namespace std;
int main()
{
int x = 200; int y = 100;
x += y; cout << x << endl; //x += y; // x = x + y, x = 200 + 100, output 300.
x -= y; cout << x << endl; //x -= y; // x = x - y, x = 200 - 100, output 100.
x /= y; cout << x << endl; //x /= y; // x = x / y, x = 200 / 100, output 2.
x %= y; cout << x << endl; //x % y; // x = % y, x = 200 % 100, output 0.
return 0;
}
我正在获取
的结果x -= y = 200
x /= y = 2
x %= y = 2
当它应该是
x -= y = 100
x /= y = 2
x %= y = 0
它实际上是将之前的结果相加。如何阻止代码将结果添加到下一行?谢谢!
每次你像x += y;
一样做赋值时,它会改变x的值。
x += y;
等同于
x = x + y;
表示赋值。 x 的值正在变化。现在是 300。后面的作业也发生了同样的事情。
注意:如果你不想改变x和y的值,最好在其他变量中赋值。
int x = 200, y = 100, t;
t = x + y; cout << t << endl;
t = x - y; cout << t << endl;
t = x * y; cout << t << endl;
t = (int) x / y; cout << t << endl;
当x
和y
是变量,U
是一个操作时x U= y
等价于:
x = x U y;
这意味着原始变量正在被修改并赋值给结果。
您的代码相当于:
x = x + y;
cout << x << endl; // x is now 300
x = x - y;
cout << x << endl; // x is now 200
x = x / y;
cout << x << endl; // x is now 2
x = x % y;
cout << x << endl; // x is now 2
如果您不想更改 x
,您可以将 x
保存在临时变量中或只打印结果:
int x = 200;
int y = 100;
cout << x - y << endl; // x unchanged
cout << x + y << endl; // x unchanged
cout << x / y << endl; // x unchanged
cout << x * y << endl; // x unchanged
或:
int x = 200;
int x = 100;
int result = x + y;
cout << result << endl;
result = x - y;
cout << x << endl;
result = x / y;
cout << result << endl;
result = x % y;
cout << result << endl;
注:由于=
运算符returns赋值后的值cout << x += y << end
可以编译打印赋值。
正如您在代码中的注释中提到的,x += y
的结果等同于 x = x + y
,这意味着 x
的值将发生变化。因此,x
的新值 300 用于下一个数学作业也就不足为奇了。
如果您想避免这种情况,请考虑将 x + y
的结果保存到另一个变量。
int x = 200;
int y = 100;
int x_addition = x + y;
std::cout << x_addition << std::endl;
或者,如果它的唯一用途是显示加法的结果,则在一行中完成。
int x = 200;
int y = 100;
std::cout << x + y << std::endl;
您没有指望加法运算值
x=200,y=100;
x+=y;//x=200+100=300,x=300,y=100
x-=y;//x=300-100=200,x=200,y=100
这里你忘记了在减法之前有加法操作
尝试在每次分配后将 x 重置回 200。像这样:
#include <iostream>
using namespace std;
int main()
{
int x = 200; int y = 100;
x += y; cout << x << endl;
x = 200;
x /= y; cout << x << endl;
x = 200;
x %= y; cout << x << endl;
return 0;
}
您希望 x
和 y
的原始值永远保留,但事实并非如此。 x += y;
或 x = x + y;
之类的赋值会更改变量的值(在本例中为 x
,即等号左侧的值)。先前的值被覆盖并因此丢失。
如果您不想更改原始值,则必须将结果分配给其他变量(例如,一个名为 temp
,每次都可以覆盖),或者您不' 完全分配它,您只需将计算结果发送到 cout
。
以临时变量为例:
int x = 200; int y = 100; int temp;
temp = x + y; cout << temp << endl; //temp = x + y, temp = 200 + 100, output 300.
temp = x - y; cout << temp << endl; //temp = x - y, temp = 200 - 100, output 100.
或者,完全取消变量:
cout << x + y << endl; // 200 + 100, output 300
cout << x - y << endl; // 200 - 100, output 100
另一种查看变量被覆盖的方法(好吧,在这种情况下它只是 x
)是将它们声明为 const
:
const int x = 200; const int y = 100;
该程序无法编译,因为它试图更改常量变量。
您正在使用累加运算符,因此您正在更改 'x' 的值。
x += y; // x = 200 + 100 = 300
x -= y; // x = 300 - 100 = 200
x /= y; // x = 200 / 100 = 2
x %= y; // x = 2 % 100 = 2
相反你可以这样做:
int x = 200;
int y = 100;
cout << x + y << endl;
cout << x - y << endl;
cout << x / y << endl;
cout << x % y << endl;
或者:
int x = 200;
int y = 100;
int result;
result = x + y;
cout << result << endl;
result = x - y;
cout << result << endl;
result = x / y;
cout << result << endl;
result = x % y;
cout << result << endl;