首页 技术 正文
技术 2022年11月15日
0 收藏 728 点赞 2,784 浏览 2275 个字

语法:for(表达式1;表达式2;表达式3){循环体;}
练习1:

  1. namespace _02.for循环的练习01
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //连续输出100次"我下次一定细心"
  8. for (int i = 1; i <= 100; i++)
  9. {
  10. Console.WriteLine("{0}.我下次会小心.",i);
  11. }
  12. Console.ReadKey();
  13. }
  14. }
  15. }

练习2:

  1. namespace _03.for循环的练习02
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //求1-100之间的所有偶数的和
  8. int sum=0; //求和
  9. for (int i = 1; i <= 100; i++)
  10. {
  11. if (i % 2 == 0)
  12. {
  13. sum += i;
  14. }
  15. }
  16. Console.WriteLine("和为:{0}.",sum);
  17. Console.ReadKey();
  18. }
  19. }
  20. }

练习3:

  1. namespace _04.for循环的练习03
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //找出100-999之间的水仙花数
  8. //所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身.
  9. //例如:153是一个“水仙花数”,因为153 = 1的三次方+5的三次方+3的三次方。
  10. int b; //保存百分位
  11. int s; //保存十分位
  12. int g; //保留个位
  13. for (int i = 100; i <=999; i++)
  14. {
  15. b = i / 100; //获得百分位
  16. s = (i / 10) % 10; //获取十分位
  17. g = i % 10; //获取个位
  18. if (Math.Pow(b, 3) + Math.Pow(s, 3) + Math.Pow(g, 3) == i)
  19. {
  20. Console.WriteLine(i);
  21. }
  22. }
  23. Console.ReadKey();
  24. }
  25. }
  26. }

练习4:

  1. namespace _05.for循环的练习04
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //输出乘法口诀表
  8. for (int i = 1; i <=9; i++)
  9. {
  10. for (int j = 1; j <=9; j++)
  11. {
  12. Console.Write(j+"*"+i+"="+i*j+"\t");
  13. if (j == i)
  14. {
  15. break;
  16. }
  17. }
  18. Console.Write("\n");
  19. }
  20. Console.ReadKey();
  21. }
  22. }
  23. }

 练习5:

  1. namespace _06.for循环的练习05
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //请用户输入一个值:
  8. //根据这个值可以输出一下加法表
  9. //0+6=6;
  10. //1+5=6;
  11. //2+4=6;
  12. //3+3=6;
  13. //4+2=6;
  14. //5+1=6;
  15. //6+0=6;
  16. Console.WriteLine("请输入一个数字:");
  17. bool b = false; //用来确认是否要运行if语句中的内容
  18. int number = 0; //用来保存输入的数字
  19. try
  20. {
  21. number = int.Parse(Console.ReadLine());
  22. b = true;
  23. }
  24. catch
  25. {
  26. Console.WriteLine("你输入的不是数字.");
  27. }
  28. if (b)
  29. {
  30. for (int i = 0, j=6; i <= 6&&j>=0; i++,j--)
  31. {
  32. Console.WriteLine(i + "+" + j + "=" + "{0}", i + j);
  33. }
  34. }
  35. Console.ReadKey();
  36. }
  37. }
  38. }

02.for循环 
breakbreak关键字的作用:(1)break;可以跳出switch语句(2)break;可以跳出当前循环.
int.Parse();  方法int.Parse();和Convert.Toint32();实际上是一个意思.区别:(1)事实上int.Parse();的运行效率比Convert.Toint32();要高,因为在程序的内部,也是将Convert.Toint32()转换成int.Parse();来操作的.(2).使用Convert.Toint32();如果转换失败,就会抛出异常.     使用int.Parse();如果转换失败,也会抛出异常.TryParse();方法的运行效率最高.
int.TryParse(); 方法尝试着将一个字符串转换成int类型.实例代码:

  1. namespace _07.TryPaese的学习
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. int number;
  8. bool b=int.TryParse("123", out number);
  9. Console.WriteLine(number);
  10. Console.WriteLine(b);
  11. Console.ReadKey();
  12. }
  13. }
  14. }

 解释上面的代码:int.TryParse();会试图将第一个参数(字符串类型),转换成int类型,然后再赋值给number,如果转换成功,转换后的值赋值给number,bool类型的b的值为true,如果转换失败,那么number会的的值为0,bool类型的b的值为false.无论转换时成功还是失败都不会抛出异常.

  1. namespace _07.TryPaese的学习
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. int number;
  8. bool b=int.TryParse("abc", out number);
  9. Console.WriteLine(number);
  10. Console.WriteLine(b);
  11. Console.ReadKey();
  12. }
  13. }
  14. }

02.for循环 
来自为知笔记(Wiz)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,989
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,503
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,131
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,842