using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
static void Main(string[] args)
Console.WriteLine("使用While結構計算");
int oddSum = 0,evenSum=0, i = 1;
while (i <= 99)
oddSum += i;//這裡的i為奇數,從1開始,此表示式用於求奇數的和
i += 1;//i變為了偶數
evenSum += i;//用於求偶數和
i += 1;//i變為了下一個奇數
}
Console.WriteLine("從1到100的奇數和為{0},偶數和為{1}", oddSum, evenSum);
Console.WriteLine();
Console.WriteLine("使用do While結構計算");
oddSum = 0;
evenSum = 0;
i = 1;
do
oddSum += i;
i += 1;
evenSum += i;
} while (i <= 99);
Console.ReadKey();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("使用While結構計算");
int oddSum = 0,evenSum=0, i = 1;
while (i <= 99)
{
oddSum += i;//這裡的i為奇數,從1開始,此表示式用於求奇數的和
i += 1;//i變為了偶數
evenSum += i;//用於求偶數和
i += 1;//i變為了下一個奇數
}
Console.WriteLine("從1到100的奇數和為{0},偶數和為{1}", oddSum, evenSum);
Console.WriteLine();
Console.WriteLine("使用do While結構計算");
oddSum = 0;
evenSum = 0;
i = 1;
do
{
oddSum += i;
i += 1;
evenSum += i;
i += 1;
} while (i <= 99);
Console.WriteLine("從1到100的奇數和為{0},偶數和為{1}", oddSum, evenSum);
Console.ReadKey();