C#第三节课(2)
2016-03-06 16:38
357 查看
运算符
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace yunsuanfu
{
class Program
{
static void Main(string[] args)
{
//算术运算符
//++ --
//放置在变量名前面或者后面
//int a = 1;
//int b=a++;//int b=a;a=a+1;
//int c=++a;//int a=a+1;c=a;
//Console.WriteLine(a);
//Console.ReadLine();
//* / %
//% 模 除商取余
//int a = 10;
//int b = 3;
//Console.WriteLine(a%b);
//+ -
//int a = 3;
//int b = 4;
//Console.WriteLine(a - b);
//关系运算符
//> < >= <=
//== !=
//int a = 4;
//int b = 6;
//bool c = a < b;
//Console.WriteLine(c);
//逻辑运算符
//&& 两个都满足
//|| 至少有一个满足
//! 若开始时是true,改为false
//int a = 4;
//int b = 7;
//int c = 8;
//bool d = a < b && a < c;
//bool e = a < b || a < c;
//Console.WriteLine(d);
//Console.WriteLine(e);
//条件运算符 ? :
//int a = 3;
//int b = 6;
//string s = a < b ? "对,a<b" : "错,a>b";
//int c = a > b ? 1 : 2;
//Console.WriteLine(s);
//赋值运算符
//= += -= /= %=
//int a = 3;
//a += 2;//a=a+2;
//a -= 5;//a=a-5;
//a *= 2;//a=a*2;
//Console.WriteLine(a);
//条件运算符,练习
//问,现在几点了。
//输入小时数,判断是am还是pm
//若是pm的,需要减去12小时进行打印报时
//Console.Write("现在几点了");
//int shi = int.Parse(Console.ReadLine());
//if (shi > 0 && shi < 24)
//{
// string mm = shi > 12 ? "现在是pm" + (shi - 12) : "现在是am" + shi;
// Console.WriteLine(mm);
//}
//else {
// Console.WriteLine("您的输入有误");
//}
//输入一个100以内的数,判断这个数是否跟7有关
//7的倍数 %
//个位数是7 %
//十位数是7 /
for ( int i ; ; )
{
Console.WriteLine("请输入一个100以内的整数:");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("是七的倍数么?" + (a % 7 == 0));
Console.WriteLine("是七的倍数么?" + (a % 10 == 7));
Console.WriteLine("是七的倍数么?" + (a / 10 == 7));
if ((a % 7 == 0) || (a % 10 == 7) || (a / 10 == 7))
{
Console.WriteLine("拍手");
}
else
{
Console.WriteLine("喊" + a);
}
}
Console.ReadLine();
}
}
}
分类 | 符号 | 解释 | 优先级 |
算数 | ++ -- | 加加 减减 | 由高到低,即执行顺序由上到下.(圆括号的优先级最高) |
*/% | 乘 除 取余 | ||
+ - | 加 减 | ||
关系 | > < >= <= | 大于 小于 大于等于 小于等于 | |
== != | 等于 不等于 | ||
逻辑 | && | 与(并且) | |
|| | 或 | ||
! | 非(注:优先级在此表的最顶层) | ||
条件运算符 | ?: | 惟一的一个三元运算符 如果 | |
赋值 | = += -= *= /= %= | 如:x-=4.即x=x-4; |
int a = 10,b=a++;
Console.WriteLine("a=" + a);
Console.WriteLine("b=" + b);结果:a=11 b=10
int a = 10, b=++a;
Console.WriteLine("a=" + a);
Console.WriteLine("b=" + b);结果:a=11 b=11
2.条件运算符:
例子:x=(x>12)?1:2; //假如x>12,那么就返回1,否则就返回2
相关文章推荐
- c#中在类名前面加上public有什么用
- C#第三节课(1)
- C# 在指定字符串中间插入换行符
- 对于新手用c#中的delegate(委托)和event(事件)
- c#---写文件
- 转载:C# 之泛型详解
- 用BenchmarkDotNet给C#程序做性能测试
- C#成魔之路<5>ADO.NET数据访问技术(1)
- C#第二节课
- C# 调用委托线程BeginInvoke与EndInvoke
- C#: WMI 获取远程 Windows 主机信息
- 推荐10本C#编程的最佳书籍
- 有关c#的几个问题
- C# BackgroundWorker的一个Demo
- 【C#学习】从泛型集合到强制转换,start brain storm~
- 【C#学习】ArrayList Hashtable List Dictionary 对比总结
- C#基础之三大结构
- C#机房重构——泛型
- C#-事件
- C#多线程(Thread)开发基础