---本周的ppt---
---本周的範例程式碼---
線上檢閱範例程式碼:
Ch02_02.cs
/***************************************
* Variables
* Author: S.D.Wu
* Date: 2014/2/16
**************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch02
{
class Ch02_02
{
static void Main(string[] args)
{
short x; //Declare variable x with short type
x = 2; //Assign value 2 to variable x
int y = 5; //Declare variable y and assign 5 to y
float z= 1.5f;
double w= 5.54;
bool b = true;
Console.WriteLine("x={0},y={1},z={2},w={3}",x,y,z,w);
Console.WriteLine("b={0}", b);
//calculate the storage sizes of the data types
Console.WriteLine("size of data type ");
Console.WriteLine("size of short ={0}", sizeof(short));
Console.WriteLine("size of int ={0}", sizeof(int));
Console.WriteLine("size of float ={0}", sizeof(float));
Console.WriteLine("size of double ={0}", sizeof(double));
Console.WriteLine("size of bool ={0}", sizeof(bool));
Console.Read();
}
}
}
Ch02_03.cs
/***************************************
* Operator
* Author: S.D.Wu
* Date: 2014/2/16
**************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch02
{
class Ch02_03
{
static void Main(string[] args)
{
short x; //Declare variable x with short type
x = 2; //Assign value 2 to variable x
int y = 5;
float z= 1.5f; //postfix f is needed
double w= 5.54;
//Arithmetic operators
Console.WriteLine("{0}+{1}={2}", x, y, x + y);
Console.WriteLine("{0}-{1}={2}", x, y, x - y);
Console.WriteLine("{0}*{1}={2}", x, y, x * y);
Console.WriteLine("{1}/{0}={2}", x, y, y / x);
Console.WriteLine("{1}%{0}={2}", x, y, y % x);
Console.WriteLine("{1}/{0}={2}", z, w, w / z);
// postfix incremental Operators
Console.WriteLine("x={0}", x);
Console.WriteLine("x={0}", x++);
Console.WriteLine("x={0}", x);
// prefix incremental Operators
Console.WriteLine("y={0}", y);
Console.WriteLine("y={0}", ++y);
Console.WriteLine("y={0}", y);
//Assignment and lambda expression
Console.WriteLine("x={0}, x+=2 ==> x={1}", x, x += 2);
Console.WriteLine("x={0}, x-=3 ==> x={1}", x, x -= 3);
//Logical Operator
Console.WriteLine("{0}>{1} ==> {2}", x, y, x > y); //larger than
Console.WriteLine("{0}>{1} ==> {2}", x, y, x < y); //less than
Console.WriteLine("{0}>{1} ==> {2}", x, y, x == y); //equal
Console.WriteLine("{0}>{1} ==> {2}", x, y, x != y); //unequal
Console.Read();
}
}
}
沒有留言:
張貼留言