if语句
if语句是根据测试条件是否满足来选择要执行的语句的。
语法如下:
语法1:
if (condition)
statement(s);
else //******else会向上找最近的if语句当句首,当这个if不满足的时候就执行else。
statement(s);
语法2:
if (condition1)
statement(s);
else if(condition2)
statement(s);
……
else if(conditionN)
statement(s);
else
statement(s);
练习题:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 2016_12_1课堂练习
{
class Program
{
static void Main(string[] args)
{
console.writeline(“请输入性别:取值为:\n boy,\n girl,\n gay,\n les”);
string gender = console.readline();
if (gender == “boy”)
{
console.writeline(“男孩”);
}
else if (gender == “girl”)
{
console.writeline(“女孩”);
}
else if (gender == “gay”)
{
console.writeline(“基佬”);
}
else if (gender == “les”)
{
console.writeline(“百合”);
}
else
{
console.writeline(“输入有误,不在取值范围内。”);
}
console.readkey();
//Console.WriteLine("请输入年龄");
//int age = Convert.ToInt32(Console.ReadLine());
//if (age>=0 && age<=3)
//{
// Console.WriteLine("幼儿");
//}
//else if (age >= 4 && age <= 12)
//{
// Console.WriteLine("儿童");
//}
//else if (age >= 13 && age <= 18)
//{
// Console.WriteLine("青少年");
//}
//else if (age >= 19 && age <= 30)
//{
// Console.WriteLine("青年");
//}
//else if (age >= 31 && age <= 50)
//{
// Console.WriteLine("中年");
//}
//else if (age >= 51 && age <= 100)
//{
// Console.WriteLine("老年");
//}
//else
//{
// Console.WriteLine("输入有误。");
//}
//Console.ReadKey();
}
}
}