C#整理3——运算符和语句
运算符:
一、算术运算符:
+ - * / % ——取余运算
取余运算的应用场景:
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——运算符和语句的更多相关文章
- java基础基础总结----- 关键字、标识符、注释、常量和变量、运算符、语句、函数、数组(三)
Java语言基础组成:关键字.标识符.注释.常量和变量.运算符.语句.函数.数组 一.标识符 标识符是在程序中自定义的一些名称,由大小写字母[a-zA-Z],数字[0-9],下划线[ _ ],特殊字符 ...
- 《JavaScript语言入门教程》记录整理:运算符、语法和标准库
目录 运算符 算数运算符 比较运算符 布尔运算符 二进制位运算符 void和逗号运算符 运算顺序 语法 数据类型的转换 错误处理机制 编程风格 console对象和控制台 标准库 Object对象 属 ...
- Java数据类型、变量、运算符、语句。
数据类型:整型 int long short byte小数 double float 字符 char 转义字符:\'(单引号字符) \\(反斜杠字符) \n(换行) \r(回车) \t(水平制表符,相 ...
- 2、C#基础整理(运算符、数据类型与转换、var关键字)
·运算符 数学运算符:+ - * / % 比较运算符:< > = <= >= != 返回bool值 逻辑运算符:&&并且.||或者,两者运行 ...
- JavaScript---网络编程(1)-介绍、变量、运算符与语句
JavaScript也是一种编程语言.并不是Java的分支哦. 可以直接在浏览器中运行的编程语言. JavaScript 的历史故事: 1.JavaScript语言与名称的由来(Netscape,Su ...
- JavaScript基础(语法类型转换、运算符、语句)
1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...
- c语言(3)--运算符&表达式&语句
计算机的本职工作是进行一系列的运算,C语言为不同的运算提供了不同的运算符! 1.那些运算符们 .基本运算符 算术运算符:+ - * / % ++ -- 赋值运算符:= 逗号运算符:, 关系运算符:& ...
- javascript类型转换、运算符、语句
1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...
- php复习整理1--位运算符
前言 子曰:"温故而知新,可以为师矣." php复习整理系列即是对已掌握的知识的温习,对久不使用的知识点进行重新学习,从而对php基础知识的掌握更加牢固.当然因为是重新温习, ...
随机推荐
- C#常用語法糖(Csharp Syntactic sugar)
首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1. ...
- AngularJS 的一些坑
UI的闪烁 Angular的自动数据绑定功能是亮点,然而,他的另一面是:在Angular初始化之前,页面中可能会给用户呈现出没有解析的表达式.当DOM准备就绪,Angular计算并替换相应的值.这样就 ...
- bootstrap使用中遇到的问题(二)
1.ie8不支持carousel组件, 解决方法:将jquery换为jquery1版本,具体原因不清楚~~~~~ 2.ie8不支持background-color:rgba(); 解决方法:这样写代码 ...
- 利用cookie改变背景色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- noNamespaceSchemaLocation前添加xsi
在.Net中操作xml文档,给节点添加,xsi:noNamespaceSchemaLocation属性时,不可以使用 XmlElement eleRoot = doc.CreateElement(&q ...
- 【笔记】JS中的数组方法
push()方法:可以向数组的末尾添加一个或者多个元素,并且返回新的长度 pop()方法:可以删除数组最后一个元素,并且返回被删除的元素,注意:如果数组是空的,该方法不进行任何操作,返回undef ...
- 《C++ Primer Plus 6th》读书笔记 - 第十一章 使用类
1. 运算符重载 2. 计算时间:一个运算符重载示例 3. 友元 1. 友元有三种: 友元函数 友元类 友元成员函数 4. 重载运算符:作为成员函数还是非成员函数 5. 再谈重载:一个矢量类 6. 类 ...
- arm ldr 指令
ldr 指令格式:(读取概念) ldr{条件} 1目的寄存器,2存储器地址 eg: ldr r0,[r1]; 把r1中数据值读取到r0中: ldr r0,[r1,r2];把r1+r2的数值 读取到r0 ...
- 获取电脑cpu的使用情况
using System; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void ...
- MySql优化方案
mysql优化方案总结 u Mysql数据库的优化技术 对mysql优化时一个综合性的技术,主要包括 a: 表的设计合理化(符合3NF) b: 添加适当索引(index) [四种: 普通索 ...