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

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();  
     }  
   }  
 }  


ไม่มีความคิดเห็น:

แสดงความคิดเห็น