using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo
{
class Program
{
static void Main(string[] args)
{
int C = 89;
int cShap = 90;
int sql = 60;
Console.WriteLine("cShap课和sql课的分数之差为"+(cShap-sql));
Console.WriteLine("三门课的平均分"+((C+cShap+sql)/3));
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo
{
class Program
{
static void Main(string[] args)
{
double i = -6.5;
int j = -2;
double a = i % j;
Console.WriteLine(a);
Console.ReadLine();
}
}
}
简单赋值运算符示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int age = 25;
Console.WriteLine("小明今年的年龄是" + age);
Console.WriteLine("小明再过5年的年龄是" + (age += 5));
Console.ReadLine();
}
}
}
复合赋值运算符的优势
复合赋值运算符的劣势
示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int num1 = 4, num2 = 7, num3 = 7;
Console.WriteLine("num1等于num2:" + (num1 == num2));
Console.WriteLine("num1不等于num2:" + (num1!= num2));
Console.WriteLine("num1大于num2:" + (num1 > num2));
Console.WriteLine("num1小于num2:" + (num1 < num2));
Console.WriteLine("num1大于等于num2:" + (num1>= num2));
Console.WriteLine("num1小于等于num2:" + (num1 <= num2));
Console.ReadLine();
}
}
}
如何同时使用多个关系运算符
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int num1 = 4, num2 = 7, num3 = 7;
Console.WriteLine("num1等于num2:" + (num1 == num2));
Console.WriteLine("num1大于num2大于num3:"+(num1 > num2 && num2 > num3));
Console.ReadLine();
}
}
}
逻辑运算符
&&
当第一个为真时,会判断第二是否为真,当第一个为假时,不会判断第二个是否再为真假;
&
当第一个为真时,会判断第二个为真,当第一个为假时,也会判断第二个是否为真假。
||
当第一个为真时,会判断第二是否为真,当第一个为假时,不会判断第二个是否再为真假;
|
当第一个为真时,会判断第二个为真,当第一个为假时,也会判断第二个是否为真假。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入用户名:");
String name = Console.ReadLine();
Console.WriteLine("请输入密码:");
String pwd = Console.ReadLine();
if ("mr" == name && "mrsoft" == pwd) {
Console.WriteLine("登录成功");
}
else
{
Console.WriteLine("登录失败");
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入密码:");
int pwd = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入密钥:");
int key = Convert.ToInt32(Console.ReadLine());
int pwdkey = pwd ^ key;
Console.WriteLine("加密的密码为:"+pwdkey);
Console.ReadLine();
}
}
}
移位运算符之左移位运算
移位运算符之左移位运算
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
int a = 368;
Console.WriteLine("左移运算2位为:" + (a << 2));
Console.WriteLine("右移运算2位为:"+(a >> 2));
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
int a = 368;
string result = a > 300 ? "是真的" : "假的";
Console.WriteLine(result);
Console.ReadLine();
}
}
}
使用条件运算符的注意事项
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
int a = 368;
bool result = a is object;
Console.WriteLine(result);
Console.ReadLine();
}
}
}
示例