C# 变量类型,变量转换,四则运算

来源:互联网 发布:北京入职体检费用 知乎 编辑:程序博客网 时间:2024/06/13 00:46

(1)变量类型,定义规则
1.变量分为:int/float/double/char/string/bool;
2.char类型为字符类型,只能包含一个字符,如单个字母,单个汉字,单个符号等;
3.定义一个类:
class+类名+{}
4.定义一个方法/函数。
访问修饰符+static+返回值类型+方法名+()+{}
访问修饰符:public 、private;
返回值类型:void/int/…….
5.定义一个变量。
public+变量类型+变量名;
如果在方法中使用到变量,且方法为静态static,则变量定义时也要加上static,如public static int studentNum;
(2)命名规则:
1.类 的命名规则:
首字母大写,其后每个单词首字母大写
2.变量 的命名规则:
首字母小写,其后其后每个单词首字母大写
3.方法 的命名规则:
首字母大写,其后每个单词首字母大写

(3)变量赋值规则:
1.直接在变量后面赋值;
如 public int a=3;
2.在方法内赋值;
如:

public static int a ;public static void Num(){   a=3;}

3.赋值
float-赋值应有小写f,如12.3f;
char-应有’ ‘,如‘夏’,‘a’
string-应有‘’‘’,如‘’student‘’
输入输出语句中,只有string、char类型加”“或”,如果为变量,则不需要加

1.如num为变量 num=3,则输出语句;

console.writeLine(num); 结果为3.
console.writeLine(“num”); 结果为num。

2.如num不为变量,则必须有”“

(4)四则运算
符合数学运算规则。
a=b+c;
a=b*c;
……..
(5)变量转换
1.低精度转高精度系统自动转换,如int转float。
2.高精度转低精度。
(变量类型)变量 如(int)b;
3.string与数型相互转换:
public static string str;
public static int a;
public static float b;

//str转数字型:
1. 如str=“3”; a=int.Parse(str);
2. 如str=”3.1“,b=float.Parse(str);
3. 如str=“3.1”,则不能直接转换成int型,需先转换成float型,即a=(int)float.Parse(str);

// 数字型转str(变量.ToString()):
1.str=a.ToString();
2.str=b.ToString();

(6)控制台输入进行赋值。
console.ReadLine() //输入,不管输入内容,一定为字符串类型,初始赋值定义时一定为string。

public static string str;
public static float a;

public static void Main(string[] args)
{
str=console.ReadLine(); //输入的内容赋值给字符串str;
a=float.Parse(str); //输入的内容强制转换成数字型,方便后续计算。
}

原创粉丝点击