唐河做网站/seo视频教程汇总
用goto可跳转到指定位置。
注意:
1.下面语句使用goto,语句的执行顺序发生改变,即先执行语句2,再执行语句1。
goto 标签;
语句1;
标签
语句2;
2.goto语句不能跳转到循环语句,也不能跳出类的范围。
判断输入的用户名与密码是否正确
代码
using System;namespace renwu
{class Program{static void Main(string[] args){int count = 1;login:Console.WriteLine("请输入用户名");string username = Console.ReadLine();Console.WriteLine("请输入密码");string userpwd = Console.ReadLine();if (username == "aaa" && userpwd == "123"){Console.WriteLine("登录成功");}else{count++;if (count > 3){Console.WriteLine("用户名或密码错误次数过多!退出!");}else{Console.WriteLine("用户名或密码错误");goto login;//返回login标签处重新输入用户名密码}}}}
}
运行结果