4.变量
4.1变量类型
在 C# 中,变量类型如下表所示:
数据 | 类型 | 描述 | 示例 | 说明 |
整型 | byte | 0 ~ 255 | byte num=1; | |
short | -32768 ~ 32768 | short num=300; | ||
int | -2147483648 ~ 2147483647 | int age=18; | 大部分情况下整数类型使用int | |
long | -2^63 ~ 2^63-1 | long num=10; | ||
单精度浮点型 | float | -3.4x10^38 ~ +3.4x10^38 | float f=0.0f; | |
双精度浮点型 | double | (+/-)5.0x10^-324 ~ (+/-)1.7x10^308 | double d=0.0d; | |
字符型 | char | 单个字符 (字母数字符号汉字等) | char c='a'; | 字符的值使用单引号 |
布尔型 | bool | 值为真或假 | bool b = true; | true为真,false为假 |
字符串型 | string | 多个字符 | string name="张三"; | 字符串的值使用双引号 |
4.2变量声明
变量声明的方法:
类型名 变量名;
如:
int i, j, k;
char c, ch;
float f, salary;
double d;
变量初始化:
变量名 = 值;
类型名 变量名 = 值;
如:
int d = 3, f = 5; /* 初始化 d 和 f. */
byte z = 22; /* 初始化 z. */
double pi = 3.14159; /* 声明 pi 的近似值 */
char x = 'x'; /* 变量 x 的值为 'x' */
变量初始化时的"="称为赋值符,赋值就是将值放入变量中。
变量的赋值:
变量名 = 值;
变量名 = 变量名;
示例:
整数示例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DemoInt : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int a = 1;
Debug.Log(a);
int b = 2;
Debug.Log(b);
b = a;
Debug.Log(b);
}
// Update is called once per frame
void Update()
{
}
}
控制台输出:
1
UnityEngine.Debug:Log(Object)
DemoInt:Start() (at Assets/DemoInt.cs:11)
2
UnityEngine.Debug:Log(Object)
DemoInt:Start() (at Assets/DemoInt.cs:13)
1
UnityEngine.Debug:Log(Object)
DemoInt:Start() (at Assets/DemoInt.cs:15)
浮点数示例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DemoDecimal : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
float f = 0.0f;
Debug.Log(f);
float x = 1.0f;
Debug.Log(x);
f = x;
Debug.Log(f);
}
// Update is called once per frame
void Update()
{
}
}
控制台输出:
0
UnityEngine.Debug:Log(Object)
DemoDecimal:Start() (at Assets/DemoDecimal.cs:11)
1
UnityEngine.Debug:Log(Object)
DemoDecimal:Start() (at Assets/DemoDecimal.cs:13)
1
UnityEngine.Debug:Log(Object)
DemoDecimal:Start() (at Assets/DemoDecimal.cs:15)
字符串示例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DemoString : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string s = "I am a student.";
Debug.Log(s);
}
// Update is called once per frame
void Update()
{
}
}
控制台输出:
I am a student.
UnityEngine.Debug:Log(Object)
DemoString:Start() (at Assets/DemoString.cs:11)
4.3变量运算
- 加法 +
- 减法 -
- 乘法 *
- 除法 /
变量之间也可以进行运算。
示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Operation : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int a = 1, b = 2;
int c = a + b;
Debug.Log(c); // 3
Debug.Log(a - c); // -2
Debug.Log(b * c); // 6
Debug.Log(c * 1.0 / b); // 1.5
}
// Update is called once per frame
void Update()
{
}
}
控制台输出:
3
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:12)
-2
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:13)
6
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:14)
1.5
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:15)
在上面的代码中,为什么要 c * 1.0 / b 而不是 c / b 呢?因为在 C# 中两个整型数相除得到的仍然是整型数,也就是说 c / b 的结果是1,而不是 1.5,c * 1.0 / b 是将运算中的分母转换为浮点型数,得到的结果就是 1.5。
- +=、-=、*=、/=
如果给一个变量加上或减去一个数值,表达式可以写成:a = a + 5 或者 a = a - 5。这条语句比较好理解:先用 a 的值加上或减去 5,然后赋值给变量 a。在 C# 中,提供了更加简洁的运算符 +=、-=、*=、/=,通过如下写法即可实现上面的操作。
变量 += 值|变量;
变量 -= 值|变量;
变量 *= 值|变量;
变量 /= 值|变量;
示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Operation : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int a = 8, b = 2;
a += b;
Debug.Log(a); // 10
a *= b;
Debug.Log(a); // 20
a -= b;
Debug.Log(a); // 18
a /= b;
Debug.Log(a); // 9
}
// Update is called once per frame
void Update()
{
}
}
控制台输出:
10
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:12)
20
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:14)
18
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:16)
9
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:18)
- ++、--
在 C# 中,将变量值加1或减1,可以使用 ++ 或 --,称为自增运算符或自减运算符。格式:
变量++;
变量--;
++变量;
--变量;
变量出现在运算符前后表示 “先使用变量再执行操作” 或者 “先执行操作再使用变量值”。
示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Operation : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int a = 8, b = 2;
Debug.Log(a++); // 输出8,a的值变化为9
Debug.Log(a); // 输出9
Debug.Log(++b); // b的值变化为3,输出3
Debug.Log(a--); // 输出9,a的值变化为8
Debug.Log(a); // 输出8
Debug.Log(--b); // b的值变化为2,输出2
}
// Update is called once per frame
void Update()
{
}
}
控制台输出:
8
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:11)
9
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:12)
3
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:13)
9
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:14)
8
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:15)
2
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:16)
- 字符串连接
字符串也可以使用 + 和 += 运算符,其结果为将字符串连接起来,如:"abc" + “def" 的结果为:"abcdef"。
示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Operation : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string a = "Happy ", b = "new year!";
Debug.Log(a + b);
a += b;
Debug.Log(a);
}
// Update is called once per frame
void Update()
{
}
}
控制台输出:
Happy new year!
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:11)
Happy new year!
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:13)
- 字符串与数值连接
+ 和 += 运算符也可以用于字符串与数值的链接,此时,数值被当作字符串来处理。
示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Operation : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string a = "Happy ";
int b = 123;
float c = 0.05f;
Debug.Log(a + b);
Debug.Log(a + c);
Debug.Log(a + b + c);
}
// Update is called once per frame
void Update()
{
}
}
控制台输出:
Happy 123
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:13)
Happy 0.05
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:14)
Happy 1230.05
UnityEngine.Debug:Log(Object)
Operation:Start() (at Assets/Operation.cs:15)