运算符:

一、算术运算符:
+ - * / % ——取余运算

取余运算的应用场景:
1.奇偶数的区分。

2.把数变化到某个范围之内。——彩票生成。

3.判断能否整除。——闰年、平年。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class2
{
//输入一个年份判断是闰年,还是平年。
static void ccc(string[] args)
{
int year;
//输入
Console.Write("请输入一个年份:");
year = Convert.ToInt32(Console.ReadLine()); //运算
//能被400整除;或能被4整除,但不能被100整除。
if ((year % == ) || (year % == && year % != ))
{
Console.WriteLine("是闰年");
}
else
{
Console.WriteLine("是平年");
}
}
}
}

++(自增运算) --(自减运算)——它只能对变量进行运算。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = ;
a++;
//7++; //错误。
Console.WriteLine(a);//a = 6;
}
}
}

1.前自增/前自减
先进行自增/自减运算,然后再进行其它运算。可以简单认为前自增/前自减的优先级是最高。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = , b;
b = ++a;
Console.WriteLine("a=" + a + ";b=" + b); //结果应当a=6,b=6
}
}
}

2.后自增/后自减
先进行其它运算,当其它运算都完成后,再进行自增/自减运算。可以简单认为是后自增/后自减优先级是最低的。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = , b;
b = a++;
Console.WriteLine("a=" + a + ";b=" + b);//结果应当是a=6,b=5
}
}
}

二、关系运算符:——用来判断式子成立与否
== != > >= < <=
注意:
双等号不要写成单等号

三、逻辑运算符:&&,||都双操作数,!单操作数
&& 与(并且)

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = , b = ;
Console.WriteLine(a > b && a > ); //false;
//true???
}
}
}

|| 或(或者)

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = , b = ;
Console.WriteLine((a > b) || (a > )); //true
//false??
}
}
}

! 非 ——取反

优先级:
一般来说:
1.算术运算术的优先级要高关系运算符;关系运算符的优先级要高于逻辑运算符
2.逻辑非优先级最高。逻辑与要高于逻辑或。
3.如果在不确定,就加小括号。

四、其它运算符:
1.赋值运算符:=。把右边的结果送到左边去。左边只能是变量。
2.复合运算符:+= -= *= /= %= 知道就行。
a+=5; <==> a = a + 5
3.条件运算符:三目运算符 ?: 。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a=,b=,c;
c = a > b ? a : b;
Console.WriteLine( c ) }
}
}

作业:
1.游泳池

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const double PI = 3.14;
const int BAR_UNIT_PRICE = ;
const int BRICK_UNIT_PRICE = ;
//输入
int a, b;
Console.Write("请输入泳池半径:");
string s1 = Console.ReadLine();
a = Convert.ToInt32(s1);
Console.Write("请输入广场半径:");
string s2 = Console.ReadLine();
b = Convert.ToInt32(s2); //运算
double l = * PI * a; //求泳池的周长
double area1 = PI * a * a;//泳池的面积。
double area2 = PI * b * b;//总面积。
double area = area2 - area1;//广场面积
double barPrice = l * BAR_UNIT_PRICE; //护栏的总造价
double brickPrice = area * BRICK_UNIT_PRICE;//广场的造价。 //输出
Console.WriteLine("护栏的长度是" + l + "米,广场砖的总面积是" + area + "平米,总造价为" + (barPrice + brickPrice) + "元"); }
}
}

2.老狼老狼几点了

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Write("老狼老狼几点了?");
string hour = Console.ReadLine();
int a = Convert.ToInt32(hour ),b;
string c; b = a > ? a - : a;
c = a > ? "下午" : "上午";
Console.WriteLine("现在是"+c+b+"点了"); }
}
}

3.输入三个数a,b,c。输出最大的。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class1
{
static void Main(string[] args)
{
int a, b, c,d,max;
Console.Write("请输入三个数:");
string a1 = Console.ReadLine();
string b1 = Console.ReadLine();
string c1 = Console.ReadLine(); a = Convert.ToInt32(a1);
b = Convert.ToInt32(b1);
c = Convert.ToInt32(c1);
d = a > b ? a : b;
max = d > c ? d : c;
Console.WriteLine("三个数中最大的数是"+max); }
}
}

二、语句:
顺序,分支,循环。

(一)顺序:略
分支:判断--表达式。if(){}
四大类:
.if
if (age > 18)
{
Console.WriteLine("可以去当兵!");
}

注意:if表达式后面只管一句话,可以省略掉{};如果if表达式后面需要管多句话,则必须加{}

.if...else...
if (age > 18)
{
Console.WriteLine("成年了!");
Console.WriteLine("可以去当兵!");
}
else
{
Console.WriteLine("还没长大!");
Console.WriteLine("回家上学去!");
}
注意:
1.else后面不要加分号。
2.else后面不要加小括号。

.if...else if...else if...else 多分支。

.if嵌套。
if(...)
{
if(...)
{
}
else
{
}
}
else
{
if(...)
{
}
else
{
}
}
分层、分类来解决问题的思路。
练习:
1.老狼几点了。凌晨,上午,下午,晚上。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class1
{
static void Main(string[] args)
{
//输入
Console.Write("老狼老狼几点了?");
string s = Console.ReadLine();
int hour = Convert.ToInt32(s); if (hour >= && hour < ) // 0<hour<6:错误
{
Console.WriteLine("凌晨" + hour + "点了");
}
else if (hour >= && hour <= )
{
Console.WriteLine("上午" + hour + "点了");
}
else if (hour > && hour < )
{
hour -= ;
Console.WriteLine("下午" + hour + "点了");
}
else if (hour >= && hour < )
{
hour -= ;
Console.WriteLine("晚上" + hour + "点了");
}
else
{
Console.WriteLine("不可识别的时间!");
} }
}
}

2.判断一元二次方向根的情况。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class1
{
//判断一元二次方程根的情况。
static void Main(string[] args)
{
int a, b, c;
//输入;
Console.Write("请输入系数a:");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入系数b:");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入系数c:");
c = Convert.ToInt32(Console.ReadLine()); //运算输出
if (a == )
{
Console.WriteLine("不是一元二次方程");
}
else
{
int d = b * b - * a * c;
if (d > )
{
Console.WriteLine("两个不等实根");
}
else if (d == )
{
Console.WriteLine("两个相等实根");
}
else
{
Console.WriteLine("无实根");
}
}
}
}
}

3.输入一个年份,判断是闰年还是平年。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class2
{
//输入一个年份判断是闰年,还是平年。
static void Main(string[] args)
{
int year;
//输入
Console.Write("请输入一个年份:");
year = Convert.ToInt32(Console.ReadLine()); //运算
//能被400整除;或能被4整除,但不能被100整除。
if ((year % == ) || (year % == && year % != ))
{
Console.WriteLine("是闰年");
}
else
{
Console.WriteLine("是平年");
}
}
}
}

4.称体重。

男人的标准体重是:体重(kg)=身高(cm)-100。
女人的标准体重是:体重(kg)=身高(cm)-110。
上下浮动3公斤属正常
要求输入性别、身高和体重,输出正常,偏胖,偏瘦

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class3
{
static void Main(string[] args)
{
string sex;
int weight, height;
//输入
Console.Write("性别(男,女):");
sex = Console.ReadLine();
Console.Write("身高(CM):");
height = Convert.ToInt32(Console.ReadLine());
Console.Write("体重(KG):");
weight = Convert.ToInt32(Console.ReadLine()); int biaoZhunTiZhong=;
//运算输出
if(sex == "男")
{
//求标准体重
biaoZhunTiZhong = height - ;
}
else if(sex == "女")
{
//求标准体重
biaoZhunTiZhong = height - ;
}
else
{
Console.WriteLine("性别不正确");
} //求体重差
int tiZhongCha = weight - biaoZhunTiZhong;
if (tiZhongCha >= - && tiZhongCha <= )
{
Console.WriteLine("体重正常,继续保持!");
}
else if (tiZhongCha < -)
{
Console.WriteLine("偏瘦,注意增加营养");
}
else
{
Console.WriteLine("偏胖,注意运动减肥");
}
}
}
}

5.输入年、月、日,判断是否是个正确的日期。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class4
{
static void Main(string[] args)
{
int year = , month = , day = ;
//输入
Console.Write("请输入年:");
year = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入月:");
month = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入日:");
day = Convert.ToInt32(Console.ReadLine()); //判断运算输出
//判断年份是否正确
if(year < || year>)
{
Console.WriteLine("年输入不正确");
}
else
{
Console.WriteLine("年正确");
} //判断月份是否正确
if (month < || month > )
{
Console.WriteLine("月输入不正确");
}
else
{
Console.WriteLine("月正确");
} //判断天是否正确
if(month==||month==||month==||month==||month==||month==||month==)
{
if(day < || day>)
{
Console.WriteLine("天输入错误,大月份最多是31天");
}
else
{
Console.WriteLine("天正确");
}
}
else if (month == || month == || month == || month == )
{
if (day < || day > )
{
Console.WriteLine("天输入错误,小月份最多是30天");
}
else
{
Console.WriteLine("天正确");
}
}
else if(month == )
{
//闰年与平年的判断
if(year%== || year%==&&year%!=)
{
//闰年
if (day < || day > )
{
Console.WriteLine("天输入错误,闰年2月份最多是29天");
}
else
{
Console.WriteLine("天正确");
}
}
else
{
//平年
if (day < || day > )
{
Console.WriteLine("天输入错误,平年2月份最多是28天");
}
else
{
Console.WriteLine("天正确");
}
}
}
}
}
}

C#整理3——运算符和语句的更多相关文章

  1. java基础基础总结----- 关键字、标识符、注释、常量和变量、运算符、语句、函数、数组(三)

    Java语言基础组成:关键字.标识符.注释.常量和变量.运算符.语句.函数.数组 一.标识符 标识符是在程序中自定义的一些名称,由大小写字母[a-zA-Z],数字[0-9],下划线[ _ ],特殊字符 ...

  2. 《JavaScript语言入门教程》记录整理:运算符、语法和标准库

    目录 运算符 算数运算符 比较运算符 布尔运算符 二进制位运算符 void和逗号运算符 运算顺序 语法 数据类型的转换 错误处理机制 编程风格 console对象和控制台 标准库 Object对象 属 ...

  3. Java数据类型、变量、运算符、语句。

    数据类型:整型 int long short byte小数 double float 字符 char 转义字符:\'(单引号字符) \\(反斜杠字符) \n(换行) \r(回车) \t(水平制表符,相 ...

  4. 2、C#基础整理(运算符、数据类型与转换、var关键字)

    ·运算符 数学运算符:+ - * / % 比较运算符:<   >   =   <=  >=   !=  返回bool值 逻辑运算符:&&并且.||或者,两者运行 ...

  5. JavaScript---网络编程(1)-介绍、变量、运算符与语句

    JavaScript也是一种编程语言.并不是Java的分支哦. 可以直接在浏览器中运行的编程语言. JavaScript 的历史故事: 1.JavaScript语言与名称的由来(Netscape,Su ...

  6. JavaScript基础(语法类型转换、运算符、语句)

    1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...

  7. c语言(3)--运算符&表达式&语句

    计算机的本职工作是进行一系列的运算,C语言为不同的运算提供了不同的运算符! 1.那些运算符们 .基本运算符 算术运算符:+ - * /  % ++ -- 赋值运算符:= 逗号运算符:, 关系运算符:& ...

  8. javascript类型转换、运算符、语句

    1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...

  9. php复习整理1--位运算符

    前言    子曰:"温故而知新,可以为师矣." php复习整理系列即是对已掌握的知识的温习,对久不使用的知识点进行重新学习,从而对php基础知识的掌握更加牢固.当然因为是重新温习, ...

随机推荐

  1. C# 数据的序列化存取

    1,什么是序列化? 序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区中读取或反序列 ...

  2. 使用Marshal.Copy把Txt行数据转为Struct类型值

    添加重要的命名空间: using System.Runtime.InteropServices; 先建立结构相同(char长度相同)的Struct类型用于转换: [StructLayout(Layou ...

  3. 为net-snmp添加读readTimeTicks

    function readTimeTicks(time){ if(time === 0) return ''; var d = 0, h = 0, m = 0, s = 0; d = parseInt ...

  4. 如何合并相同数据并转置(mysql)实现

    上次参加天猫大数据竞赛 是预测用户会买哪些牌子给用户推荐 拥有的字段 user_id,brand_id 最后要转成如下格式,比如用户1,要给他推荐2,3,4号品牌 原来的数据是 user_id,bra ...

  5. ps 网页布局

    910  1680  找一个页面作为参考   双击小手回到正常视角 新建组  把他们放到一个组里  新建组改名(创意专家)  放入一个图片 内发光投影  Shiftalt  复制 新建组  改名创意案 ...

  6. hdu2309ICPC Score Totalizer Software

    Problem Description The International Clown and Pierrot Competition (ICPC), is one of the most disti ...

  7. [zz]npm安装错误解决方法

    错误: npm ERR! at Object.parse (native) npm ERR! at Packer.readRules (/usr/local/lib/node_modules/npm/ ...

  8. inline-block布局方式

    刚研究了一下inline-block布局方式 inline-block布局方式是一种比float浮动更优的一种布局方式. 一个超简单的demo html: <!doctype html> ...

  9. yii操作数据库(PDO)

    1.数据访问对象(DAO): 执行 SQL 语句 数据库连接建立后,SQL 语句就可以通过使用 [CDbCommand] 执行了.你可以通过使用指定的SQL语句作为参数调用 [CDbConnectio ...

  10. C语言基础06

    函数: 一组特定功能的代码段,之所以使用函数,为了在文件多处需要同一段代码时可以多次重复利用,减少代码冗余. //函数的声明 返回值类型 函数名称 ( 数据类型 形参1,数据类型 ,形参2 ) ; / ...