C# goto语句的具体使用
作者:花伤情犹在 时间:2021-07-22 22:26:22
C# goto
语句用于直接在一个程序中转到程序中的标签指定的位置,标签实际上由标识符加上冒号构成。
语法形式如下。
goto Labell;
语句块 1;
Labell
语句块 2;
如果要跳转到某一个标签指定的位置,直接使用 goto 加标签名即可。
在上面的语句中使用了 goto
语句后,语句的执行顺序发生了变化,即先执行语句块 2,再执行语句块 1。
此外,需要注意的是 goto
语句不能跳转到循环语句中,也不能跳出类的范围。
由于 goto
语句不便于程序的理解,因此 goto
语句并不常用。
【实例】使用 goto
语句判断输入的用户名和密码是否正确,如果错误次数超过3次,则输出“用户名或密码错误次数过多!退出!”。
根据题目要求,假设用户名为 aaa、密码为 123,代码如下。
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标签处重新输入用户名密码
}
}
}
}
执行上面的代码,效果如下图所示。
来源:https://blog.csdn.net/qq_31762741/article/details/114963033
标签:C#,goto语句
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java批量修改文件后缀名方法总结
2022-03-15 15:46:04
![](https://img.aspxhome.com/file/2023/5/62655_0s.png)
一文搞懂Java ScheduledExecutorService的使用
2022-11-22 14:23:35
Jar包冲突问题原理及解决方案
2023-03-05 09:40:02
![](https://img.aspxhome.com/file/2023/2/64112_0s.png)
Mybatis获取参数值和查询功能的案例详解
2023-02-09 13:25:14
Java中自动生成构造方法详解
2023-06-21 14:17:44
深入理解Java设计模式之命令模式
2023-11-24 11:06:31
![](https://img.aspxhome.com/file/2023/4/59684_0s.jpg)
详解Kotlin:forEach也能break和continue
2022-05-03 01:24:10
解决@JsonInclude(JsonInclude.Include.NON_NULL)不起作用问题
2023-05-26 08:57:45
![](https://img.aspxhome.com/file/2023/0/67470_0s.png)
详解MyBatis的Dao层实现和配置文件深入
2022-07-26 02:29:17
![](https://img.aspxhome.com/file/2023/6/61246_0s.jpg)
Spring基于注解的缓存声明深入探究
2023-01-20 13:26:06
Java数组索引异常产生及解决方案
2023-11-05 16:52:27
![](https://img.aspxhome.com/file/2023/2/58702_0s.png)
Java实现简单文件过滤器功能
2021-09-13 23:27:58
![](https://img.aspxhome.com/file/2023/1/62111_0s.png)
mybatis注解与xml常用语句汇总
2022-05-17 18:39:47
Jmeter分布式压力测试实现过程详解
2022-03-23 21:32:44
![](https://img.aspxhome.com/file/2023/0/63040_0s.png)
spring boot实现自动输出word文档功能的实例代码
2021-11-10 13:37:51
![](https://img.aspxhome.com/file/2023/9/67459_0s.png)
Java使用HttpUtils实现发送HTTP请求
2021-06-11 07:08:39
![](https://img.aspxhome.com/file/2023/4/61034_0s.jpg)
详解Mybatis是如何解析配置文件的
2023-10-15 23:23:40
![](https://img.aspxhome.com/file/2023/0/61930_0s.png)
解析SpringBoot中使用LoadTimeWeaving技术实现AOP功能
2023-04-19 00:21:47
![](https://img.aspxhome.com/file/2023/1/69671_0s.jpg)
SpringBoot预加载与懒加载实现方法超详细讲解
2021-08-31 11:39:13
浅谈java项目与javaweb项目导入jar包的区别
2023-11-11 11:06:19