C# 使用循环与递归的方式计算从1加到100
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyTest
{
internal class Program
{
static void Main(string[] args)
{
Calculator c = new Calculator();
int Sum1 = c.SumFrom1Tox(100);
int Sum2 = c.SumDg(100);
Console.WriteLine(Sum1);
Console.WriteLine(Sum2);
Console.ReadLine();
}
}class Calculator
{
public int SumFrom1Tox(int x)
{
int Sum=0;
for (int i = 1; i < x+1; i++) //循坏
{
Sum = Sum + i;
}
return Sum;
}
public int SumDg(int x)
{
if (x == 1)
{
return 1;
}
else
{
int Sum=x+ SumDg(x - 1); //递归
return Sum;
}
}
}
}
原文地址:https://blog.csdn.net/Freesial_/article/details/140720757
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!