一、复习
1、自增与自减
int x,y
x = -1
y = x++ + ++x
Console.WriteLine(y)
2、除号
int a = 3
int b = 2
Console.WriteLine(a / b)
double c = 3
Console.WriteLine(a/ c)
3、@
string str = @"qqqqq
qqqqqq\t\n"
Console.WriteLine(str)
4、字符串连接
Console.WriteLine("1"+"+"+"2"+"="+"3");
二、输入、转制类型转换、练习,循环等
1、输入 Console.Read()、Console.ReadLine()
int get1= Console.Read()
Console.WriteLine(get1+2)
string get2= Console.ReadLine()
Console.WriteLine(get2)
2、强制类型转换 Convert.ToInt32()
string get2= Console.ReadLine()
int num1= Convert.ToInt32(get2)
Console.WriteLine(num1+10)
3、强制类型转换的例题,输入斤数得出总价
int aa = 3, bb = 4, cc = 5;
Console.Write("请输入苹果购买的斤数:");
int num01 = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入橙子购买的斤数:" );
int num02 = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入香蕉购买的斤数:");
int num03 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("总价:"+(num01*aa+num02*bb+num03*cc))
4、交换值,定义一个中间值
int sub1=23,sub2=21;//sub1的值与sub2的值交换
int cen = sub2
sub2 = sub1
sub1 = cen
Console.Write(sub1)
Console.Write(sub2)
5、格式化输出(当有很多变量时)
Console.Wirte("苹果单价为{0},香蕉单价为{1},橙子单价为{2}",aa,bb,cc);
6、格式化输出例题,输入一个整数,输出个位和十位
int num01 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(num01+"的个位数是"+num01%10+"十位数是"+num01/10);
Console.WriteLine(num01+"的个位数是{0}十位数是{1}",num01%10,num01/10);
7、运算符
(1)、布尔bool > < == >= <= !=
bool check01 = 4 > 5
bool check02 = 6 < 4
bool check03 = 7== 5
bool check04 = 7>= 5
bool check05 = 7<= 5
bool check06 = 7!= 5
Console.WriteLine(check01)
Console.WriteLine(check02)
Console.WriteLine(check03)
Console.WriteLine(check04)
Console.WriteLine(check05)
Console.WriteLine(check06)
(2)、赋值运算符 = += -= *= /= %= (a+=b <--> a=a+b)
int num1 = 1
int num2 = 2
num2 += num1
Console.WriteLine(num2)
(3)、逻辑运算符 &&与(有假为假),||或(有真为真),!非(取反)
Console.WriteLine(3 > 4 && 4 > 3);
Console.WriteLine(3 > 4 || 4 > 3);
Console.WriteLine(!(4 > 3);
8、条件分支语句
(1)、单条件分支语句 就两种情况
int num1 = 3
int num2 = 5
if(num1 > num2)
{
Console.WriteLine("num1大")
}
else
{
Console.WriteLine("num2大")
}
(2)、多条件分支语句 多种情况
a、输入年龄,大于60免票,14-59全票,小于14半票
Console.WriteLine("请输入您的年龄");
int arg = Convert.ToInt32(Console.ReadLine());
if(arg >= 60)
{
Console.WriteLine("免费");
}
else if(14<arg && arg < 59)
{
Console.WriteLine("全票");
}
else
{
Console.WriteLine("半票");
}
b、输入考试成绩,小于60-D,61-69-C,70-89-B,90-100-A;
Console.WriteLine("请输入考试成绩");
int cj = Convert.ToInt32(Console.ReadLine());
if (cj < 60)
{
Console.WriteLine("D");
}
else if (cj < 69)
{
Console.WriteLine("C");
}
else if (cj < 89)
{
Console.WriteLine("B");
}
else if (cj < 100)
{
Console.WriteLine("A");
}
else
{
Console.WriteLine("输入成绩无效");
}
c、输入坐标判断是第几象限
Console.WriteLine("情输入坐标值");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
if (a != 0 && b != 0)
{
if (a > 0 && b > 0)
{
Console.WriteLine("第一象限");
}
else if (a > 0 && b < 0)
{
Console.WriteLine("第四象限");
}
else if(a < 0 && b < 0)
{
Console.WriteLine("第三象限");
}
else if (a < 0 && b > 0)
{
Console.WriteLine("第二象限");
}
}
else
{
Console.WriteLine("原点");
}
d、输入三角形的三条边,判断是否能组成三角形
Console.WriteLine("请输入三角形三条边");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());
if (a + b > c || b + c > a|| a+c>b)
{
Console.WriteLine("可以组成三角形");
}
else
{
Console.WriteLine("不可以组成三角形");
}
e、输入年份判断是否是闰年(被4或者400整除并且不能被100整除)
Console.WriteLine("请输入年份");
int year = Convert.ToInt32(Console.ReadLine());
if (year%4==0 ||year%400==0 && year % 100 != 0)
{
Console.WriteLine("yes");
}
else
{
Console.WriteLine("no");
}
f、输入一个非零整数,判断是正数还是负数,并输出该绝对值
Console.WriteLine("输入一个非0整数");
int a = Convert.ToInt32(Console.ReadLine());
if (a > 0)
{
Console.WriteLine("正数"+a);
}
else
{
a = -a;
Console.WriteLine("负数,绝对值为" + a);
}
g、输入三个整数,输出最大数的平方
Console.WriteLine("输入3个整数");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());
if (a > b && a > c)
{
Console.WriteLine(a*a);
}
else if (b > a && b > c)
{
Console.WriteLine(b * b);
}
else if (c > a && c > b)
{
Console.WriteLine(c * c);
}
(3)、switch语句
int num = Convert.ToInt32(Console.ReadLine());
switch (num)
{
case 1:
Console.WriteLine(1); break;
case 2:
Console.WriteLine(2); break;
default:
Console.WriteLine(3); break;
}