1.if语句的一般形式
Console.WriteLine("请自己的名字:");
string name = Console.ReadLine();
if (name == "赵文娟")
{
Console.WriteLine("是我的名字");
}
else
{
Console.WriteLine("不是我的名字");
}
2.if嵌套语句(这种用法少,因为层次感不强)
int num = Convert.ToInt32(Console.ReadLine());
if (num > 0)
{
if (num >=50)
{
Console.WriteLine("num>=50");
}
else
{
if (num <30)
{
Console.WriteLine("num<30");
}
else
{
Console.WriteLine("num>=30,num<50");
}
}
}
else
{
Console.WriteLine("num<=0");
}
3.if的另一种写法,这种用法常见
int num = Convert.ToInt32(Console.ReadLine());
if (num < 0)
{
Console.WriteLine("num<0");
}
else if (num > 100)
{
Console.WriteLine ("num>100");
}
else if (num > 50)
{
Console.WriteLine("num>50");
}
else
{
Console.WriteLine("num>0");//最后这个else可以不写,但是要为了习惯好,还是写
}
4.用逻辑运算符(&&两个都成立才可以为true)
int num_1 = Convert.ToInt32(Console.ReadLine());
int num_2 = Convert.ToInt32(Console.ReadLine());
if (num_1 > 0 && num_2 > 0)//只用看第一个,第一个不成立就不成立
{
Console.WriteLine("ok");
}
else
{
Console.WriteLine("error");
}
5.输入三个数字,比较哪个大
Console.WriteLine("输入三个数");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());
if (a > b && a > c)
{
Console.WriteLine(a);
}
else if (b > c)
{
Console.WriteLine(b);
}
else
{
Console.WriteLine(c);
}
6.分数系统
int a = Convert.ToInt32(Console.ReadLine ());
if (a >= 90&&a<=100)
{
Console.WriteLine("优秀");
}
else if (a > 100)
{
Console.WriteLine("不合要求");
}
else if (a>=80)
{
Console.WriteLine("良好");
}
else if (a >= 70)
{
Console.WriteLine("中等");
}
else if (a >= 60)
{
Console.WriteLine("及格");
}
else
{
Console.WriteLine("不及格");
}
或
Console.WriteLine("输入你的成绩");
int a = Convert.ToInt32(Console.ReadLine());
if (a >0 && a <= 100)
{
switch (a / 10)
{
case 6:
Console.WriteLine("及格");
break;
case 7:
Console.WriteLine("中等");
break;
case 8:
Console.WriteLine("良好");
break;
case 9:
Console.WriteLine("优秀");
break;
default:
Console.WriteLine("不及格");
break;
}
}
else
{
Console.WriteLine("error");
}
7.进入系统
static void Main()
{
Console.WriteLine("欢迎进入VIP系统");
Console.WriteLine("--------------------");
Console.WriteLine("请输入密码:");
int k = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("再次确认密码");
int j = Convert.ToInt32(Console.ReadLine());
if (k == j)
{
Console.WriteLine("两次密码一至,请输入实名:");
string name = Console.ReadLine();
if (name == "zhao")
{
Console.WriteLine("欢迎进入!");
}
else
{
Console.WriteLine("名字不真实,不欢迎你!请退出!");
}
}
else
{
Console.WriteLine("两次密码不一样,退出登陆");
}
}
8.日期运行
int nian= Convert.ToInt32(Console .ReadLine ());
Console.WriteLine ("年");
if (nian <= 0 )
{
Console.WriteLine("您输入的年不符合标");
}
int yue = Convert.ToInt32(Console .ReadLine ());
Console.WriteLine("月");
if (yue <= 0 )
{
Console.WriteLine("您输入的月不符合标");
}
int ri = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("日");
if (ri <= 0)
{
Console.WriteLine("您输入的日不符合标");
}
if (yue >= 1 && yue <= 12)
{
if (yue == 1 || yue == 3 || yue == 5 || yue == 7 || yue == 8 || yue == 10 || yue == 12)
{
if (ri > 31)
{
Console.WriteLine("error");
}
}
else if (yue != 2 && ri >= 30)
{
Console.WriteLine("error");
}
else if (yue == 2 && ri > 29)
{
Console.WriteLine("error");
}
else if (nian % 4 != 0 && ri > 28)
{
Console.WriteLine("error");
}
}
9.switch语句的基本格式
Console.WriteLine("请输入a,b,c其中一个字母:");
string s = Console.ReadLine();
switch (s)
{
case "a"://是冒号
Console.WriteLine("你输入的字母是a");
break;//跳转语法
case "b":
Console.WriteLine("你输入的字母是b");
break;
case "c":
Console.WriteLine("你输入的字母是c");
break;
default:
Console.WriteLine("输入的内容不符要求");
break;
}
10. Console.WriteLine("请输入1,2,3其中一个字母:");
int s =Convert.ToInt32(Console.ReadLine());
switch (s)
{
case 1:
Console.WriteLine("你输入的字母是a");
break;
case 2:
Console.WriteLine("你输入的字母是b");
break;
case 3:
Console.WriteLine("你输入的字母是c");
break;
default:
Console.WriteLine("输入的内容不符要求");
break;
}
11.考察运行时按4,直接跳出
Console.WriteLine("1.查询,2.取款,3.修改,4.退出");
int a=Convert.ToInt32(Console.ReadLine());
switch (a)
{
case 1:
Console.WriteLine("查询");
break;
case 2:
Console.WriteLine("取款");
break;
case 3:
Console.WriteLine("修改");
break;
case 4:
break;//跳出了整个switch语句
default:
Console.WriteLine("输入的不合要求");
break;
}
12.Console.WriteLine("2008是不是闰年");
int a = Convert.ToInt32(Console.ReadLine());
//a = 2008;
if (a % 4 != 0&&a%100!=0||a%400==0)
{
Console.WriteLine("不是闰年");
}
else
{
Console.WriteLine("是闰年");
}