วันอาทิตย์ที่ 4 พฤษภาคม พ.ศ. 2557

C# class example

โปรแกรมจะรับค่า properties ของ air จำนวน 10 ชุด
โดยจะมี class AirCondition เพื่อเป็นต้นแบบของวัตถุ
เมื่อรับค่าเสร็จก็จะแสดงค่า BTU ของ air แต่ละตัวผ่าน method getBTU()
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace ConsoleApplication2  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       List<AirCondition> air = new List<AirCondition>();  
       int n = 1, width, high, length, var;  
       while (n <= 10)  
       {  
         Console.WriteLine("Enter properties of air{0}/10 :", n);  
         Console.WriteLine("Enter width : ");  
         string s_width = Console.ReadLine();  
         Console.WriteLine("Enter high : ");  
         string s_high = Console.ReadLine();  
         Console.WriteLine("Enter length : ");  
         string s_length = Console.ReadLine();  
         Console.WriteLine("Enter var : ");  
         string s_var = Console.ReadLine();  
         if (int.TryParse(s_width, out width) && int.TryParse(s_high, out high) && int.TryParse(s_length, out length) && int.TryParse(s_var, out var))  
         {  
           AirCondition temp = new AirCondition(width, high, length, var);  
           air.Add(temp);  
           n++;  
         }  
         else  
         {  
           Console.WriteLine("Please enter again!!");  
         }  
       }  
       n = 1;  
       foreach (AirCondition ob in air)  
       {   
         Console.WriteLine("BTU of air{0}/10 : {1}", n, ob.getBTU());  
         n++;  
       }  
       Console.WriteLine("Press any key to stop...");  
       Console.ReadKey();  
       /*AirCondition air = new AirCondition(5,3,5,400);  
       double value = air.getBTU();  
       Console.WriteLine("BTU : {0}", value);  
       Console.WriteLine("Press any key to stop...");  
       Console.ReadKey();  
       */  
     }  
   }  
   class AirCondition  
   {  
     public int width;  
     public int high;  
     public int length;  
     public int var;  
     public AirCondition()  
     {  
       width = 0;  
       high = 0;  
       length = 0;  
       var = 0;  
     }  
     public AirCondition(int width,int high, int length, int var)  
     {  
       this.width = width;  
       this.high = high;  
       this.length = length;  
       this.var = var;  
     }  
     public double getBTU()  
     {  
       return (width * high * length * var) / 3;  
     }  
   }  
 }  

--------------------------------------------------

C# get input number from keyboard and find max min


โปรแกรมจะรับ input จาก keyboard จำนวน 10 ตัวเลข
จะมีการเช็ค input ว่าเป็นตัวเลขหรือไม่ ถ้าไม่ใช่ก็จะให้กลับไปใส่อีกรอบ
เมื่อใส่ตัวเลขเรียบร้อยแล้วก็จะแสดงผลค่ามากที่สุดและน้อยที่สุด
ผ่านหน้าจอโปรแกรม
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace ConsoleApplication1  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       int[] numbers;  
       numbers = new int[10];  
       int n = 1, value, max, min;  
       while (n <= 10)  
       {  
         Console.WriteLine("Enter number {0}/10 :", n);  
         string line = Console.ReadLine();  
         if (int.TryParse(line, out value))  
         {  
           numbers[n - 1] = value;  
           n++;  
         }  
         else  
         {  
           Console.WriteLine("Please enter again!!");  
         }  
       }  
       max = min = numbers[0];  
       foreach (int i in numbers)   
       {  
         if (i > max)   
         {  
           max = i;  
         }  
         if (i < min)   
         {  
           min = i;  
         }  
       }  
       Console.WriteLine("\n\nmax number : {0} \nmin number : {1}",max,min);  
       Console.WriteLine("Press any key to stop...");  
       Console.ReadKey();  
     }  
   }  
 }