总结:C#变量,占位符等相关知识

时间:2022-08-16 22:56:58

新年耽误了不少时间,好久没认真的坐下来学习了,新年也快完了,又要开始正式学习了,按着视频教学学习,用了一天的时间,学习了下简单的变量及其相关的输入输出和应用,学了几种最基本的类型:

int(整型) char(字符型) string(字符串类型)double(双精度浮点数) decimal(货币值类型) float(浮点数)。

Main方法中,不允许重复申明变量,但可以重复赋值,重复赋值以后原来的变量值被顶替为新赋的值。

一.  在C#中,“+” 有两种含义;

1.联值符号,当+左右两边只要有一边是字符或者字符串类型的时候,用“+”表示连接左右两边的数据。

2.数学中的加号,参与运算的是字符型的数据,表示进行数学上的加法运算。

赋值运算符=(不是数学中的等于符号),是C#中最低的运算等级,在最后执行。

二. 占位符

第一个{0}

第二个{1}

第三个{2}

.......

例如:Console.WriteLine("姓名{0} 性别{1} 年龄{2}",name,sex,age);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 输出变量与联值
{
class Program
{
static void Main(string[] args)
{
string name;
name = "张三";
int age = ;
age = ; //重复赋值变量age的值。
decimal pay = 7600.33m; //Console.Write("我叫"+name);
//Console.Write(",今年"+age+"岁,");
//Console.Write("我的工资是"+pay+"元."); //Console.WriteLine("我叫"+name+",今年"+age+"岁,"+"我的工资是"+pay+"元.");
Console.WriteLine("我叫{0},今年{1}岁,我的工资是{2}元.", name, age, pay);//{0}{1}{2}表示占位符。占位符可以重复使用,可以省略。 Console.WriteLine("我叫"+name,"今年"+age+"岁了.");//逗号前为第一个参数,console输出逗号前的第一个参数。
Console.WriteLine("{0}我叫" + name, "今年" + age + "岁了.");//{0}"今年" + age + "岁了."代替前面的占位符的变量。 int a = ;//同为数字类型的用“+”表示数学上的加法。
//string a = "1"; 联值符号的用法区别,左右两边只要一边有字符或者字符串类型用“+”就是联值符号。
int b = ;
Console.WriteLine(a+b);
Console.WriteLine("1+2");
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 变量作业
{
class Program
{
static void Main(string[] args)
{
string name = "张三";
string Tel = "";
char sex = '男';
int age = ;
Console.WriteLine("{0},{1},{2},{3}",name,Tel,sex,age);
Console.ReadKey(); }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 变量作业4
{
class Program
{
static void Main(string[] args)
{
string Pho = "SAMSUNG";
string type = "I9300";
decimal money = 3799m;
string weight = "0.3kg";//double weight = 0.3;
Console.WriteLine("我的手机牌子是{0},型号是{1},手机价格是{2}元,重量是{3}",Pho,type,money,weight); Console.ReadKey();
}
}
}

Console.ReadLine();用于接收用户输入的数据,需要定义一个字符串类型(string)的变量来存储用户的变量。

  string input;

  input=Console.ReadLine();

等价于 string input=Console.ReadLine();

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 用户输入
{
class Program
{
static void Main(string[] args)
{
// string input;
Console.WriteLine("输入这句话的前面");
Console.WriteLine("请在这里输入一句话!");
string input = Console.ReadLine();
Console.WriteLine("输入这句话的后面");
Console.ReadKey(); }
}
}

三. 交换变量数值

若要相互交换两个变量的数值,需要借助第三个变量来完成。

  int a =5,b=10;

  int c;

  c = b;

  b = a;

  a = c;

  Console.WriteLine("a={0} b={1}",a,b);

  Console.ReadKey();

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 交换变量
{
class Program
{
static void Main(string[] args)
{ //交换两个变量的算法,需要介助第三个变量。
int a = ;
int b = ;
int c;
c = a;
a = b;
b = c; Console.WriteLine("a={0} b={1}",a,b); Console.WriteLine("a={0} b={1}",b,a);//并不会交换两个变量的值
Console.ReadKey();
}
}
}

写了这么多,写博客复习,总结了下今天的学习,还是蛮有充实感的。突然有这种感触,别人都已经是这方面的高手了,自己才刚开始,想想那句闻道有先后,真是说到心坎里去了,没有其他办法,只有加油了。