运算符:

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

取余运算的应用场景:
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. JS~JS里的数据类型

    JS里的数据类型,它虽然是个弱类型的语言,但它也有自己的规定的,它不会向其它语言那么,使用int来声明一个整形变量,而是使用 var,如果你是一个C#的开发者,你就会知道,原来C#现在也在和JS学,开 ...

  2. UVA 12232 - Exclusive-OR(带权并查集)

    UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...

  3. C# 封装-属性

    属性使封装更容易 可以使用属性(properties),这些方法对其他对象来说就像是字段,可以用属性来获取或设置一个后备字段,后备字段就是由属性所设置的一个字段名 private int number ...

  4. wpf中,一个简单的自定义treeview

    首先创建一个自定义控件,在里面定义好treeview的样式,将本来的三角形的图标变为加号的图标,并且添加节点之间的连线. <UserControl x:Class="TreeViewE ...

  5. 如何测试sql语句性能,提高执行效率

    有时候我们经常为我们的sql语句执行效率低下发愁,反复优化后,可还是得不到提高 那么你就用这条语句找出你sql到底是在哪里慢了 示例: SET STATISTICS io ON        SET ...

  6. 世界国家名与英文名【json】

    英文版 var geolocation= [   ["AO", "Angola"],   ["AF", "Afghanistan& ...

  7. python基础之 list和 tuple(元组)

    list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ...

  8. git-svn 的使用

    从 SVN 克隆代码 git svn clone https://192.168.1.3/svn/project-name   git-svn 初始化 git svn init (svn remote ...

  9. 多线程程序中fork导致的一些问题

    最近项目中,在使用多线程和多进程时,遇到了些问题. 问题描述:在多线程程序中fork出一个新进程,发现新的进程无法正常工作. 解决办法:将开线程的代码放在fork以后.也就是放在新的子进程中进行创建. ...

  10. avalon中常用的事件

     ms-on-change 相当于失去焦点事件. ms-on-input 相当于watch事件 http://www.runoob.com/jsref/event-oninput.html