简单的初学者C++,递增和递减问题
Simple beginner C++, increment and decrement questions
我正在使用C++,并且我的增量和递减非常稳定,但是我有一个方程,我必须递减theChar
方程,或者int var
减 -2 的方程,我不知道它的代码。
请更好地表述您的问题。你的意思是什么
我有一个方程,我必须递减"theChar"方程,或者 带有"int var"的那个 -2
你的意思是:
char x = 'a';
x = x + 3; //now x is 'd'
int var = 10;
var -= 2; //equal to var = var -2;
方程不是数学意义上的方程。
=
符号告诉计算机将右侧的内容存储到左侧的变量中。
int a;
a = 5;
这会将5
存储到a
一次。
int a, b;
a = 5;
b = a;
a = 6;
b
仍然是5
,因为当它被存储时,它是从a
复制的。当a
发生变化时,b
没有重新计算,而是保持原样。
int a;
a = 5;
a = a - 2;
a
现在正在减少2
,因为a
被设置为5
当计算右侧(a - 2
)时,它被计算为3
。完成后,它被写入a
的左侧,因此此时a
被3
覆盖;
char c = 'B';
c = c - 1;
c
在此代码末尾的值为 'A'
。有一些幕后魔术正在发生。字符也是数字。那么当我'B'
存储到计算机实际存储66
变量中时,真正会发生什么。你可以在这里阅读。当 i 递减 1 时,该值从 66
递减为 65
.带有数字65
的字符恰好是'A'
。
我在评论中读到您很难将其全部放入程序中。我继续为你写了一个代码片段。
#include <iostream>
using namespace std;
int main()
{
int a, b;
char c;
//cout is like a friend that you give something to put on the console
// << means give this to cout
cout << "Hello World!" << endl; //endl is a new line character
cout << endl << "Setting a, b" << endl;
a = 5;
b = a;
cout << "Value of a is " << a << ". " << "Value of b is " << b << "." << endl;
cout << endl << "Changing a" << endl;
a = 3;
cout << "Value of a is " << a << ". " << "Value of b is " << b << "." << endl;
cout << endl << "Adding to a" << endl;
a = a + 3;
cout << "Value of a is " << a << ". " << "Value of b is " << b << "." << endl;
cout << endl << "Playing around with characters" << endl;
c = 'B';
cout << "Character c is " << c << ". " << "Number stored for c is actually " << (int)c << "." << endl;
c = c + 1;
cout << "Character c is " << c << ". " << "Number stored for c is actually " << (int)c << "." << endl;
c = 70;
cout << "Character c is " << c << ". " << "Number stored for c is actually " << (int)c << "." << endl;
}
相关文章:
- 嵌套for循环C++的问题(初学者)
- 初学者问题:如何使用这些晦涩难懂的命令,例如C++中的'RenderWindow'
- 初学者问题:如何访问此变量?
- 您好,我是C++初学者,有这种问题.它给了我倍增的错误
- 初学者问题:C++指针/地址 - 和变量之后不是以前?
- 初学者关于数组和指针的问题
- 模板编程初学者问题
- 在 Rcpp-Function 中使用布尔向量的子集(Rcpp 初学者的问题..)
- 我在C 初学者中发现了无限的循环问题
- 在C 中的初学者,我面临此问题错误C3867
- 我的代码(c ++ 初学者)有什么问题?
- 初学者 - 我的"produce word backwards"程序有什么问题?
- 如果/else语句未显示/可变分配问题的输出;初学者程序员
- 有关循环冗余代码的初学者问题
- 初学者问题 - 致命错误LNK1561:必须定义入口点
- 将功能与"int main"问题分开(初学者)
- 这种矢量编码有什么问题?(初学者)
- C++练习车辆程序问题 - 初学者编程
- for循环的问题(初学者)
- 获取dll路径/字符串问题(c++初学者)