作业帮 > 综合 > 作业

1、编写一个方法,计算:1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 10 + 11 -

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/29 00:35:05
1、编写一个方法,计算:1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 10 + 11 -
用C#写出代码 最好有注释
using System; using System.Text; namespace Mynamespace { public class Program { public static int main(){ int temp=0; while(temp==0){ Console.WriteLine(" input any number more than 0,and then count"); Console.Read( temp); } Sytem.Int32 count=0; System.Int32 a =1; while( a++!=temp) { if (a%2==0) count-=a; else count+=a; } if(temp%2==0) count-=temp; else count+=temp; Console.WriteLine("after count and the result is {0}",count); }}}