break
1);用于switch case中时跳出switch
2);用在循环中时跳出循环
int age = 0, sum = 0;
int i;
for (i = 0; i < 5;i++ )
{
try
{
Console.WriteLine("请输入第{0}个人岁数", i + 1);
age = Convert.ToInt32(Console.ReadLine());
if (age < 0 || age > 100)
{
Console.WriteLine("不合法");
break;
}
}
catch
{
Console.WriteLine("请输入数字类型");
break;
}
sum += age;
}
if(i<5==false)
{
Console.WriteLine("平均数为{0}", sum / 5);
}
Console.ReadKey();
string userName, passWord;
while (true) //死循环 只能配合break用
{
Console.WriteLine("请输入你的用户名");
userName = Console.ReadLine();
Console.WriteLine("请输入你的密码");
passWord = Console.ReadLine();
if(userName=="admin"&&passWord=="888888") //用户名和密码输对就跳出循环 跟常规写法相反 慢慢体会
{
break;
}
Console.WriteLine("输入有误");
}
Console.WriteLine("登录成功");//用户名和密码输对就跳到此处
Console.ReadKey();
int sum = 0;
int i = 0;
for (i = 1; i <= 100; i++)
{
sum += i;
if(sum>=20)
{
break;
}
}
Console.WriteLine(i);
Console.ReadKey();