// For Loop
for(int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
int [] myNumbers = {10, 20, 30, 40, 50};
for(int i = 0; i < myNumbers.Length; i++)
{
Console.WriteLine(myNumbers[i]);
}
/*--------------------------------------------------------------------------------------------------*/
// Foreach Loop
char [] message = {'H', 'e', 'l', 'l', 'o'};
foreach(char i in message)
{
Console.WriteLine(i);
}
/*--------------------------------------------------------------------------------------------------*/
// While
int counter = 5;
while(counter > 0)
{
Console.WriteLine("Counter = {0}", counter);
counter--;
}
/*--------------------------------------------------------------------------------------------------*/
// Do while
int counter = 100;
do
{
Console.WriteLine("Conter = {0}", counter);
counter++;
}
while (counter < 0);
/*--------------------------------------------------------------------------------------------------*/
// break
for(int i = 0; i < 5; i++)
{
Console.WriteLine("i = {0}", i);
if(i == 2)
break;
}
/*--------------------------------------------------------------------------------------------------*/
// continue
for(int i = 0; i < 5; i++)
{
Console.WriteLine("i = {0}", i);
if(i == 2)
continue;
Console.WriteLine("I will not be printed if i = 2.\n");
}
/*--------------------------------------------------------------------------------------------------*/
//異常處理 Exception Handing
int numerator, denominator;
Console.Write("Please enter numerator: ");
numerator = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter denominator: ");
denominator = Convert.ToInt32(Console.ReadLine());
try //嘗試
{
Console.WriteLine("The result is {0}", numerator / denominator);
}
catch(Exception e) //例外的處理方法
{
Console.WriteLine(e.Message);
}
finally //不論是否發生例外都會執行
{
Console.WriteLine("--- End of Error Handling Example ---");
}
/*--------------------------------------------------------------------------------------------------*/
//特定異常處理
catch(DivideByZeroException e) //除數等於零
{
Console.WriteLine(e.Message);
}
int numerator, denominator;
try
{
Console.Write("Please enter numerator: ");
numerator = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter denominator: ");
denominator = Convert.ToInt32(Console.ReadLine());
}
catch(FormatException e)
{
Console.WriteLine("Error: You did not enter an integer");
}
catch(DivideByZeroException)
{
Console.WriteLine(e.Message);
}
/*--------------------------------------------------------------------------------------------------*/
//聲明屬性
private string nameOfStaff;
private const int hourlyRate = 30;
private int hWorked;
public int HoursWorked
{
get
{
return hWorked;
}
set
{
if(value > 0)
{
hWorked = value;
}
else
{
hWorked = 0;
}
}
}
//如果沒有任何邏輯,可以使用以下簡短資料
public int HoursWorked {get; set;}
/*--------------------------------------------------------------------------------------------------*/
//聲明方法
public void PrintMessage()
{
Console.WriteLine("Calculating Pay...");
}
public int CalculatePay()
{
PrintMessage();
int staffPay;
staffPay = hWorked * hourlyRate;
if(hWorked > 0)
return staffPay;
else
return 0;
}
/*--------------------------------------------------------------------------------------------------*/
// C#允許不同方法使用同名(參數需不同)
public int CalculatePay()
{
PrintMessage();
int staffPay;
staffPay = hWorked * hourlyRate;
if(hWorked > 0)
return staffPay;
else
return 0;
}
publice int CalculatePay(int bonus, int allowance)
{
PrintMessage();
int staffPay;
staffPay = hWorked * hourlyRate + bonus + allowance;
if(hWorked > 0)
return staffPay;
else
return 0;
}
/*--------------------------------------------------------------------------------------------------*/
// ToString()的方法
//重寫方法 override
public override string ToString()
{
return "Name of Staff = " + nameOfStaff + " Hour Rate = " + hourlyRate +
" Hours Worked = " + hWorked;
}
沒有留言:
張貼留言