首页 > 代码库 > 用循环语句求1+2+3+……+100
用循环语句求1+2+3+……+100
1.用while语句
1 using System; 2 class Sum100 3 { 4 static void Main() 5 { 6 int Sum, i; 7 Sum = 0; i = 1; 8 while (i<=100) 9 {10 Sum += i;11 i++;12 }13 Console.WriteLine("Sum is" + Sum);14 }15 }
2.do-while语句
1 using System; 2 class Sum100 3 { 4 static void Main() 5 { 6 int Sum, i; 7 Sum = 0; i = 1; 8 do 9 {10 Sum += i;11 i++;12 }13 while (i<=100)14 Console.WriteLine("Sum is" + Sum);15 }16 }
3.for语句(1)
1 using System; 2 3 class Sum100 4 { 5 static void Main() 6 { 7 int Sum, i; 8 Sum = 0; 9 for (i = 1; i <= 100;i++ )10 Sum += i;11 Console.WriteLine("Sum is" + Sum);12 13 Sum = 0;14 for (i = 100; i > 0;i-- )15 Sum += i;16 Console.WriteLine("Sum is" + Sum);17 }18 }
4.for语句(2)
1 using System; 2 3 class Sum100 4 { 5 static void Main(string[] args) 6 { 7 int Sum, i; 8 9 for (Sum = 0,i = 1; i <= 100;i++ )10 Sum += i;11 Console.WriteLine("Sum is" + Sum);12 13 14 for (Sum = 0,i <= 100; Sum+i,i++);15 16 Console.WriteLine("Sum is" + Sum);17 }18 }
5.for语句(3)
1 using System; 2 class Sum100 3 { 4 static void Main(string[] args) 5 { 6 int Sum, i; 7 8 for (Sum = 0, i = 1; i <= 100; ) 9 Sum += i++;10 Console.WriteLine("Sum is" + Sum);11 12 13 for (Sum = 0,i =1; ;Sum+=i,i++)14 if(i>100) break;15 Console.WriteLine("Sum is" + Sum);16 17 Sum=0;i=1;18 for(;;)19 {20 Sum+=i++;21 if (i>100)22 break;23 }24 Console.WriteLine("Sum is" + Sum);25 }26 }
用循环语句求1+2+3+……+100
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。