All posts by 龙生
C# 知识点 goto【学习笔记】
定义
1 |
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> 语句将程序控制直接传递给标记语句。 |
1 |
优点:具有了超强的灵活性<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />弱点:降低代码的可读性。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />结构化编程所关注的就是解决单个功能函数内部的功能实现,所强调的是每个函数有“一个入口和一个出口”,而goto语句的使用却似乎违背了这样的准则。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />但是,如果我们把goto语句限制在一个函数体内部使用,那么,goto语句还是会带来很多便利的。或许正是由于这一点,C#的缔造者也依然保留了功能强大的goto语句。 |
备注
1 |
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> 的一个通常用法是将控制传递给特定的 <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">switch</span>-<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> 标签或 <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">switch</span> 语句中的默认标签。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> 语句还用于跳出深嵌套循环。 |
可以考虑使用goto的情形
1 |
1.从多重循环中直接跳出 <br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />2. 出错时清除资源 <br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />3.可增加程序的清晰度的情况。 |
不加限制地使用goto带来的弊端
1 |
1.破坏了清晰的程序结构,使程序的可读性变差,甚至成为不可维护的"面条代码"<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />2.经常带来错误或隐患。它可能跳过了某些对象的构造、变量的初始化、重要的计算等语句 |
示例 goto 在 switch 语句中的使用。
1 |
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> statements_goto_switch.cs</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">using</span> System;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">class</span> SwitchTest<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />{<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">static</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">void</span> Main()<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Coffee sizes: 1=Small 2=Medium 3=Large</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.Write(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Please enter your selection: </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">string</span> s = Console.ReadLine();<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> n = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span>.Parse(s);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> cost = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">switch</span> (n)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">1</span>:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> cost += <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">25</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">break</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">2</span>:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> cost += <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">25</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">1</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">3</span>:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> cost += <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">50</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">1</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">default</span>:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Invalid selection.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">break</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">if</span> (cost != <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Please insert {0} cents.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>, cost);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Thank you for your business.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />} |
示例输出
1 |
Coffee sizes: 1=Small 2=Medium 3=Large<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />Please enter your selection: 2<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />Please insert 50 cents.<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />Thank you for your business. |
使用 goto 跳出嵌套循环
1 |
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> statements_goto.cs<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Nested search loops</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">using</span> System;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">public</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">class</span> GotoTest1<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />{<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">static</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">void</span> Main()<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> x = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">200</span>, y = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">4</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> count = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">string</span>[,] array = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">new</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">string</span>[x, y];<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Initialize the array:</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">for</span> (<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> i = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>; i < x; i++)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">for</span> (<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> j = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>; j < y; j++)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> array[i, j] = (++count).ToString();<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Read input:</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span> Console.Write(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Enter the number to search for: </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Input a string:</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">string</span> myNumber = Console.ReadLine();<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Search:</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">for</span> (<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> i = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>; i < x; i++)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">for</span> (<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> j = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>; j < y; j++)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">if</span> (array[i, j].Equals(myNumber))<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> Found;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">The number {0} was not found.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>, myNumber);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> Finish;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Found:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">The number {0} is found.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>, myNumber);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Finish:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">End of search.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />}<br type="_moz" /> |
1 |
FROM URL:<a href="http://www.cnblogs.com/IT-Bear/archive/2012/03/05/2380389.html" style="background-color: rgb(255, 255, 255); ">http://www.cnblogs.com/IT-Bear/archive/2012/03/05/2380389.html</a> |
C#的Escape和Unescape
基于C#的Escape和Unescape实现 //在C#后台实现JavaScript的函数escape()的字符串转换//些方法支持汉字private string escape(string s){StringBuilder sb = new StringBuilder();byte[] byteArr = System.Text.Encoding.Unicode.GetBytes(s); for (int i = 0; i < byteArr.Length; i += 2){ sb.Append("%u");sb.Append(byteArr[i + 1].ToString("X2"));//把字节转换为十六进制的字符串表现形式 sb.Append(byteArr[i].ToString("X2"));}return sb.ToString(); }//把JavaScript的escape()转换过去的字符串解释回来//些方法支持汉字private string unescape(string s){ string str = s.Remove(0, 2);//删除最前面两个"%u"string[] strArr = str.Split(new string[]{"%u"}, StringSplitOptions.None);//以子字符串"%u"分隔byte[] byteArr = new byte[strArr.Length * 2];for (int i = 0,j=0; i < strArr.Length;i++,j+=2){byteArr[j + 1] = Convert.ToByte(strArr[i].Substring(0,2), 16); //把十六进制形式的字串符串转换为二进制字节byteArr[j ] = Convert.ToByte(strArr[i].Substring(2, 2), 16);}str = System.Text.Encoding.Unicode.GetString(byteArr); //把字节转为unicode编码return str; } 下面的为不支持汉字的转换: public static string Escape(string str){if (str == null)returnString.Empty; StringBuilder sb = newStringBuilder();int len = str.Length; for […]
View DetailsIP地址的分类
A类 1.0.0.0 到126.0.0.0 0.0.0.0 和127.0.0.0保留 B 128.1.0.0到191.254.0.0 128.0.0.0和191.255.0.0保留 C 192.0.1.0 到223.255.254.0 192.0.0.0和223.255.255.0保留 D 224.0.0.0到239.255.255.255用于多点广播 E 240.0.0.0到255.255.255.254保留 255.255.255.255用于广播 非保留的地址基本上都可以用。保留的和用于广播的都属于公用地址不可使用。除了192.168.0.1你还可以设类如10.0.0.1这样的非保留地址。
View DetailsIP地址 正则表达式
IP地址的长度为32位,分为4段,每段8位,用十进制数字表示,每段数字范围为0~255,段与段之间用英文句点“.”隔开。例如:某台计算机IP地址为10.11.44.100。 分析IP地址的组成特点:250-255、200-249、0-199。 这三种情况可以分开考虑, 1. 250-255:特点:三位数,百位是2,十位是5,个位是0~5,用正则表达式可以写成:25[0-5] 2. 200-249:特点:三位数,百位是2,十位是0~4,个位是0~9,用正则表达式可以写成:2[0-4]\d 3. 0-199:这个可以继续分拆,这样写起来更加简单明了. 3.1. 0-9: 特点:一位数,个位是0~9,用正则表达式可以写成:\d 3.2. 10-99: 特点:二位数,十位是1~9,个位是0~9,用正则表达式可以写成:[1-9]\d 3.3. 100-199:特点:三位数,百位是1,十位是0~9,个位是0~9,用正则表达式可以写成:1\d{2} 于是0-99的正则表达式可以合写为[1-9]?\d,那么0-199用正则表达式就可以写成(1\d{2})|([1-9]?\d),这样0~255的正则表达式就可以写成(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))) 最后,前面3段加上句点.可以使用{3}重复得到,第4段再来一次同样的匹配,得到IP地址的正则表达式:
1 |
((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))) |
与大家互动一下: 1. 为什么三位数的匹配放在二位数/一位数的前面?因为正则表达式规则之一:最先开始的匹配拥有最高的优先权。 2. 0.0.0.0和255.255.255.255是合法存在的IP地址,你知道是为什么吗? 3. 192.169.01.108这种数字前面多带了个0的类型的,在这里不是合法的,为什么要这样? 4. 为什么前面在最前面要有?:呢?它在这里有什么用? 5. 如果您发现本表达式有错误,也请留言指正! url:http://www.cnblogs.com/txw1958/archive/2011/10/13/2210114.html
View Details钱包的颜色选择
现代人的钱包大部份都用折式的,据说钱天天弯腰,得不到舒展,不能聚财,风水书建议人们使用直版式的钱包。而使用期限越新的钱包,运气越强。一个钱包大约使用三年,运气差不多就被用光了,最好重新买一个新的,才可增加财运。 钱包色彩很讲究。 红色钱包:红色代表赤字的意思,也就是很容易把钱花光无法存到钱,或者说血本无归吧。 蓝色钱包:蓝色代表水,意思是钱会像钱一样流走,也是一种不适合钱包的颜色。 黑色钱包:黑色代表沉稳,可以守住钱财,不轻易让钱损失,尤其适合喜欢做一些小投资的人。 咖啡色钱包:有黑色钱包同样的意思,只是力量没有黑色钱包那么强。 黄色钱包:黄色同黄金、金钱。黄色、金色代表财气,是最好的钱包颜色了。对于要考虑一些大的投资项目的人来说,金色钱包能助你一臂之力。最忌红色,其次深黄,这两个颜色散财; 女性的话,粉色最好,财源滚滚(源头为异性),其次淡黄、米色和白色,黑色平稳。 白色代表净,不太适合钱包的颜色容易脏。
View Details如何在ashx页面获取Session值
在一般事务处理页面,可以轻松的得到 Request,Response对象,从而进行相应的操作,如下: HttpRequest Request = context.Request; HttpResponse Response = context.Response; 但是要得到 Session的值就没有那么简单了。比如你要在ashx得到保存在Session中的登录帐号Session["userAccount"] 如果你只是context.Session["userAccount"]的话是会报 “未将对象引用设置到对象的实例”的异常 所以,如果要想取Session中的值 ,需要如下所示 1、引入 命名空间: using System.Web.SessionState; 2、实现IRequiresSessionState接口,具体如下 /// <summary>/// $codebehindclassname$ 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class AddUserInfo : IHttpHandler,IRequiresSessionState //就是这样显示的实现一下,不用实现什么方法{ public void ProcessRequest(HttpContext context){ //… //这样你就可以如下 操作了 if(context.Session["userAccount"] != null) { string account = context.Session["userAccount"].ToString(); } //…继续下面的代码 } } FROM URL:http://www.cnblogs.com/vihone/archive/2010/06/04/1751490.html
View Details爱自己:7种方法让你感觉很棒
我总是一个叛逆者-独立,有点不合群。我以我的独立自豪,我喜欢用自己的方式做事情,而且我不关心无需的投入(委婉的说!) 我从22岁开始就是自主经营,这样维持生计也是艰难的。总的来说,我很成功因为我有能力去照顾自己并且有情感支柱。 对我来说,自爱为我的目标服务,去做我生命中想做的事,不管我是否有来自外界的支持。 其实所有的事情,我并没有完全能做到自爱。这是一个持续的过程,有些日子比另一些日子要好。在这些日子,我已经掌握了一些技巧可以提高自爱,让自己感觉很棒。 1. 列出你收到的赞美 我保证这有许多,诺贝尔奖提名的人都不被要求。事实上你烤个很惊人的馅饼或者是你是朋友在需要倾听者的时候需要的人,这些都是好的列子;所以得到一个学位或者知道如何改变你车里的油。 当你觉得自己不特别的时候,看看这些列出的单子。你取得的成绩,觉得很酷,并提醒自己,你是如何真棒。就个人而言,我喜欢在高中被评选为“最具特色的”。 2.学习新事物 你不需要变成一个整个领域的专家(除非那是有吸引力的)。学会用德语说“这是多么美好的一天”(“Habt einen schönen tag!”),听Ted讲座,或者去维基百科点“随机文章”的链接,直到你发现一些有趣的事。 把我们的注意力放在有意思的外在世界;它也可以扩展我们的世界观和价值观。此外,学习会使你的大脑变得开心。 3.问问你的好朋友/伙伴/亲人 他们最爱你哪些,看看你是如何惊人的 记下他们说的,当你觉得自己不被关心得时候想想这些。我们对自己的看法是最主要的(毕竟这叫自爱)。我们总是喜欢听到我们爱的人的赞美,这些看法是值得我们尊敬。 让我清楚的是:我不是在谈论实际上是友敌的“朋友”,或家庭成员成天对你的人生选择妄加批评。这个问题是保留给你的一个很喜欢的人,这个人会跟你志趣相投。 4.把你的注意力放在那些行善的小事 如果我有一天用来自我批判,我会变得内向,很少关注外面的世界(和花费精力善待自己--不是非常有用)。 ,如果不是出于考虑,我会努力地和我遇到的人聊天,和他们友好的交谈;我将会成为一个很体贴的司机;我会跟陌生人说“你好”。 对我来说,专注于别人,能单单提醒我们是互相联系的,也会发邮件给我系统里联系的人,我是不能接受一直玩内向和自我批评的人。 5.有时看看自己的内心 我相信我自己知道什么时候我只需要一两个小时去放空自己。没有电子邮件,互联网或其他的娱乐—只有我和一杯喝的东西,闲逛着,描绘我的未来,思考我想要什么,我要去哪里,以及如何我去到那里。 对我来说,这就像按下了重新设置按钮。它净化我的大脑,缓解一些内心的困惑,让我感觉有动力,有活力。冥想是伟大的。因此,在一个咖啡馆呆半小时不去想老板和孩子。这会是难以想象的满足。做任何适合你的。 6.放你最喜欢的音乐和舞曲 一个令人难以置信的基本概念,很简单,超级好玩,都是好孩子。我也不怕你事后觉得内啡肽抽不好,缓解压力,移动身体, 一个没约束的个性化舞会。 7.学会照顾自己 我使用的最有效的方法是有规律的自我照顾,去避免不能保持自爱的时候,我搞许多小的自我照顾行为,偶有较大的额外奉送。 早起享受我的早晨咖啡,安排自己的方式,看看日历上的日子不会使我头疼;确保我的冰箱是储藏丰富的,所以我不用橄榄和老芹菜作为晚餐—这些细节对我来说很有用,让我感觉强大,可靠。 当我的生命平稳进行时,我只觉得更好地了解自己,而且,我的生活是自己过得,所以我要对此负责。 记住:虽然我们是互相联系的,并且在许多方面是相同的,但是你,是存在唯一的你。你是独一无二的,惊人的,特殊的。陶醉它,因为你摇滚。 from url:http://xue.youdao.com/article.z?id=5339968794001963091&keyfrom=PopWindow&exist=true
View DetailsSWFUpload V2.2.0 说明文档
SWFUpload V2.2.0 说明文档 更新时间:2008-11-07 TOC SWFUpload SWFUpload 2 概述 入门 SWFUpload JavaScript 对象 构造器 全局变量和常量 instances movieCount QUEUE_ERROR UPLOAD_ERROR FILE_STATUS Default Event Handlers BUTTON_ACTION(added in v2.2.0) BUTTON_CURSOR(added in v2.2.0) BUTTON_WINDOW_MODE(added in v2.2.0) 属性 customSettings movieName 方法 addSetting (不推荐使用) getSetting (不推荐使用) retrieveSetting (v2.1.0中已删除) destroy (v2.1.0中新增) displayDebugInfo selectFile (不推荐使用) selectFiles (不推荐使用) startUpload cancelUpload stopUpload getStats setStats getFile addPostParam removePostParam addFileParam removeFileParam setUploadURL setPostParams setFileTypes setFileSizeLimit setFileUploadLimit setFileQueueLimit setFilePostName setUseQueryString setDebugEnabled setButtonImageURL (added in v2.2.0) setButtonDimensions (added in v2.2.0) setButtonText (added in v2.2.0) setButtonTextStyle (added in v2.2.0) setButtonTextPadding (added in v2.2.0) […]
View Detailsswfupload在IE9中不显示上传按钮的问题
问题描述: 使用swfupload.js上传文件,在IE8、火狐、谷歌浏览器中显示正常,但是在IE9下不能显示上传按钮。 解决方法: 打开swfupload.js文件找到307行开始的如下代码: // Private: getFlashHTML generates the object tag needed to embed the flash in to the document SWFUpload.prototype.getFlashHTML = function (flashVersion) { // Flash Satay object syntax: http://www.alistapart.com/articles/flashsatay return ['<object id="',this.movieName, '" type="application/x-shockwave-flash" data="', (this.support.imageResize ?this.settings.flash_url : this.settings.flash9_url), '" width="', this.settings.button_width, '" height="',this.settings.button_height, '" class="swfupload">', '<param name="wmode" value="',this.settings.button_window_mode, '" />', '<param name="movie" value="', (this.support.imageResize ?this.settings.flash_url : this.settings.flash9_url), '" />', '<param name="quality" value="high" />', '<param name="allowScriptAccess" value="always" />', '<param name="flashvars" value="' +this.getFlashVars() + '" />', '</object>'].join(""); }; 将其替换为: // Private: getFlashHTML generates the object tag […]
View DetailsJavascript中decodeURI()与decodeURIComponent()区别
decodeURI()函数: 定义和用法:decodeURI()函数可对encodeURI()函数编码过的URI进行解码.语法:decodeURI(URIstring)参数描述:URIstring必需,一个字符串,含有要解码的URI组或其他要解码的文本.返回值:URIstring的副本,其中的十六进制转义序列将被它们表示的字符替换. decodeURIComponent()函数: 定义和用法:decodeURIComponent()函数可对encodeURIComponent()函数编码过的URI进行解码.语法:decodeURIComponent(URIstring)参数描述:URIstring必需,一个字符串,含有解码的URI组件或其他要解码的文本.返回值:URIstring的副本,其中的十六进制转义序列将被它们表示的字符替换.
1 |
<p> </p><p><span class="tag"><html></span><br /><span class="pln"> </span><span class="tag"><body></span><br /><span class="pln"> </span><span class="tag"><script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">></span><br /><span class="pln"> </span><span class="kwd">var</span><span class="pln"> test </span><span class="pun">=</span><span class="pln"> </span><span class="str">"</span><a href="http://www.test.com/My" rel="nofollow"><span class="str">http://www.test.com/My</span></a><span class="str"> test/"</span><span class="pun">; </span><span class="pln"> </span><br /><span class="pln"> </span><span class="kwd">var</span><span class="pln"> test1 </span><span class="pun">=</span><span class="pln"> encodeURI</span><span class="pun">(</span><span class="pln">test</span><span class="pun">);</span><br /><span class="pln"> </span><span class="kwd">var</span><span class="pln"> test2 </span><span class="pun">=</span><span class="pln"> decodeURI</span><span class="pun">(</span><span class="pln">test1</span><span class="pun">);</span><br /><span class="pln"> </span><span class="kwd">var</span><span class="pln"> test3 </span><span class="pun">=</span><span class="pln"> encodeURIComponent</span><span class="pun">(</span><span class="pln">test</span><span class="pun">);</span><br /><span class="pln"> </span><span class="kwd">var</span><span class="pln"> test4 </span><span class="pun">=</span><span class="pln"> decodeURIComponent</span><span class="pun">(</span><span class="pln">test3</span><span class="pun">);</span></p><br /><p><span class="pln"> document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">test1 </span><span class="pun">+</span><span class="pln"> </span><span class="str">"<br />"</span><span class="pun">);</span><br /><span class="pln"> document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">test2 </span><span class="pun">+</span><span class="pln"> </span><span class="str">"<br />"</span><span class="pun">);</span><br /><span class="pln"> document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">test3 </span><span class="pun">+</span><span class="pln"> </span><span class="str">"<br />"</span><span class="pun">);</span><br /><span class="pln"> document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">test4 </span><span class="pun">+</span><span class="pln"> </span><span class="str">"<br />"</span><span class="pun">);</span><br /><span class="pln"> </span><span class="tag"></script></span><br /><span class="pln"> </span><span class="tag"></body></span><br /><span class="tag"></html></span></p><p> </p> |
运行结果: http://www.test.com/My%20test/ http://www.test.com/My test/ http%3A%2F%2Fwww.test.com%2FMy%20test%2Fhttp://www.test.com/My test/
View Details