C#基础(4)——break语句、continue语句、while循环、do-while循环、for循环、foreach循环
2017-10-23 18:40
901 查看
break、while
break是跳出当前循环,不是所有循环。using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChuangzhiConsel { class Program { static void Main(string[] args) { int i = 1; int j = 1; while (i <= 10) { while (j <= 10) { Console.WriteLine("我是最里层"); j++; break; } Console.WriteLine("我是最外层----"); i++; } Console.ReadKey(); } } }
但是,break不单独使用,而与if配合使用,当满足一定条件就不再循环了。如:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChuangzhiConsel { class Program { static void Main(string[] args) { string answer = "n"; int i = 1; while (answer == "n" && i <= 10) { Console.WriteLine("这是第{0}次讲解,你会了吗?y/n", i); answer = Console.ReadLine(); if (answer == "y") { Console.WriteLine("你终于会了!放学了。。。"); break; } i++; } if (i == 11) { Console.WriteLine("还不会,第{0}次了!放弃吧!", i-1); } Console.ReadKey(); } } }
continue
do-while循环
先执行一遍,再拿执行后的结果,再循环的,推荐do-while,即先做再判断:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChuangzhiConsel { class Program { static void Main(string[] args) { string input = "n"; do { if (input != "n") { Console.WriteLine("要输入n"); } Console.WriteLine("y/n?"); input = Console.ReadLine(); } while (input == "n"||input!="y"); Console.ReadKey(); } } }
for循环
打for顺序循环双击tab,代码格式化输出。打forr逆序循环双击tab,代码格式化输出。
变步长:for (int i = 0; i < 100; i+=2)
求水仙花数:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChuangzhiConsel { class Program { static void Main(string[] args) { int hundred = 0; int decade = 0; int unit = 0; for (int i = 100; i <= 999; i++) { hundred = i / 100; decade = i % 100 / 10; unit = i % 10; if (Math.Pow(hundred, 3) + Math.Pow(decade, 3) + Math.Pow(unit, 3) == i) { Console.WriteLine("水仙花数为:{0}", i); } } Console.ReadKey(); } } }
九九乘法表
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChuangzhiConsel { class Program { static void Main(string[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <=9; j++) { Console.Write("{0}*{1}={2}\t", i, j,i * j); } Console.WriteLine(); } Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChuangzhiConsel { class Program { static void Main(string[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <=i; j++) { Console.Write("{0}*{1}={2}\t", i, j,i * j); if (i == j) { Console.WriteLine(); } } } Console.ReadKey(); } } }
质数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChuangzhiConsel { class Program { static void Main(string[] args) { //怎么判断质数(素数) Console.WriteLine("输入大于2的数字:"); int length = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("质数:1"); for (int i = 2; i <= length; i++) { bool is_prime = true; for (int j = 2; j < i; j++) { if (i % j == 0) { is_prime = false; break;//不是质数 } } if (is_prime) { Console.WriteLine("质数:{0}", i); } } Console.ReadKey(); } } }
相关文章推荐
- javass 视频笔记三 switch语句 for循环,while循环,do-while循环,break和continue
- [置顶] 循环语句if(){}else{}、if(){}else if(){}...else{}、do{}while()、for(){}、with(){}break/continue语句
- 05:for循环、break和continue、while、do-while、二重循环
- 4 Java基础语法(switch语句,循环语句(for,while),控制跳转语句(break,return,continue))
- js---javaScript(循环语句的使用实例)for while do....while(转移语句): break, continue , return
- JavaScript之基础-7 JavaScript 循环结构 (while、do-while、for及循环中的continue、break)
- JAVA 基础语法(四)——循环结构(while,do...while,for,break,continue)
- python笔记5 - 字符串格式化表达式,while语句嵌套,for循环,break,continue,死循环
- JAVA流程控制语句(顺序语句、判断、选择判断语句、while循环、do while循环、for循环、break、continue关键字)
- java的选择结构switch和循环结构for while do while以及流程控制语句break continue return
- if,continue,break,while,do-while, switch,return,foreach,for等条件语句的使用
- JavaSE (七)循环控制语句:while、do....while、for,break和continue用法
- java基础3 循环语句:While 循环语句、do while 循环语句、 for 循环语句 和 break、continue关键字
- 【C#基础】while循环和do-while循环
- Python条件判断 if-else for循环 while循环 break continue
- C#中foreach,for,while,Do-While循环
- 19 C#循环语句的跳过和中断 continue和break
- C#中循环语句:while、for、foreach的使用
- C#中foreach,for,while,Do-While循环对比
- 04-01 Java switch、for、while、do while语句,循环嵌套,跳转语句break、continue、return