asp.net中for和do迴圈語句用法分享

來源:互聯網
上載者:User

本樣本的FOR迴圈建立一個Mandelbrot映像。 複製代碼 代碼如下:using System;
namespace a
{
class Program
{
public static void Main(string[] args)
{
double realCoord,imagCoord;
double realTemp,imagTemp,realTemp2,arg;
int iterations;
for (imagCoord=1.2;imagCoord>=-1.2;imagCoord-=0.05)
{
for(realCoord=-0.6;realCoord<=1.77;realCoord+=0.03)
{
iterations=0;
realTemp=realCoord;
imagTemp=imagCoord;
arg=(realCoord*realCoord)+(imagCoord*imagCoord);
while((arg<4)&&(iterations<40))
{
realTemp2=(realTemp*realTemp)-(imagTemp*imagTemp)-realCoord;
imagTemp=(2*realTemp*imagTemp)-imagCoord;
realTemp=realTemp2;
arg=(realTemp*realTemp)+(imagTemp*imagTemp);
iterations+=1;
}
switch (iterations % 4)
{
case 0:
Console.Write(".");
break;
case 1:
Console.Write("o");
break;
case 2:
Console.Write("0");
break;
case 3:
Console.Write("@");
break;
}
}
Console.Write("n");
}
Console.ReadKey();
}
}
}

迴圈結構之DO語句
迴圈結構之DO語句,根據布爾值的測試結果,執行相應代碼,DO語句至少執行一次。 複製代碼 代碼如下:using System;
namespace a
{
class Program
{
public static void Main(string[] args)
{
double balance,interestRate,targetBalance;
Console.WriteLine("What is your current balance?");
balance=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("What is your current annual interest rate (in %)?");
interestRate= 1+Convert.ToDouble(Console.ReadLine())/100.0;
Console.WriteLine("What balance would you like to have?");
targetBalance=Convert.ToDouble(Console.ReadLine());
int totalYears=0;
do
{
balance*=interestRate;
++totalYears;
}
while(balance<targetBalance);
Console.WriteLine("In {0} year {1} you'll have a balance of {2}.",totalYears,totalYears==1?"":"s",balan00ce);
Console.ReadKey();
}
}
}

總結
DO語句在迴圈時不管條件是否成立它至少執行一次,頁for迴圈語句如果條件不成立一次不執行了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.