1 / 12

Console Input and Output

Console Input and Output. Suphot Sawattiwong tohpus@gmail.com. Reference: http://siamclassview.edu.chula.ac.th/pongedu/view.php?Page=1245824319677881. System.Console.WriteLine(). คำสั่งแสดงผลข้อความบน Console โดยหลังจากแสดงเสร็จจะทำการขึ้นบรรทัดใหม่ทันที

myra-hurley
Download Presentation

Console Input and Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Console Input and Output SuphotSawattiwong tohpus@gmail.com Reference: http://siamclassview.edu.chula.ac.th/pongedu/view.php?Page=1245824319677881

  2. System.Console.WriteLine() • คำสั่งแสดงผลข้อความบน Console โดยหลังจากแสดงเสร็จจะทำการขึ้นบรรทัดใหม่ทันที • สามารถจัด Format หรือรูปแบบข้อมูลต่างๆได้ Console.WriteLine("Hello World"); Console.WriteLine(17); Console.WriteLine("x= " + x);

  3. System.Console.Write() • คำสั่งแสดงผลข้อความบน Console โดยหลังจากแสดงเสร็จจะไม่มีการขึ้นบรรทัดใหม่ทันที • สามารถจัด Format หรือรูปแบบข้อมูลต่างๆได้ Console.Write("Hello World"); Console.Write(17); Console.Write("x= " + x);

  4. Formatting String •  ในการแสดงผลลัพธ์ ที่มากกว่า 1 ตัวนั้นบางครั้งเราต้องการแสดงผลลัพธ์ของนิพจน์พร้อมกับการจัดรูปแบบการแสดงผล เช่น การระบุตำแหน่ง การระบุความกว้าง จำนวนตัวอักษร การจัดรูปแบบชิดซ้ายหรือชิดขวา เป็นต้น

  5. Formatting String (ต่อ) • เราสามารถทำสิ่งเหล่านี้ได้โดยการใช้วิธีการกำหนดรูป ซึ่งในภาษา C# จะเรียกว่า  สตริงกำหนดรูปแบบ โดยมีรูปแบบดังนี้ • index เป็นจำนวนเต็มเริ่มที่ 0 ใช้สำหรับระบุอาร์กิวเมนต์ที่ต้องการจัดรูปแบบ • alignment เป็นจำนวนเต็มที่ใช้สำหรับระบุความกว้างหรือจำนวนตัวอักษร และ การจัดการแสดงผลแบบชิดซ้าย หรือชิดขวา โดยถ้า alignment มีค่าติดลบ จะชิดซ้าย และถ้า มีค่าเป็นบวกจะชิดขวา โดย 1 หน่วยของ alignment +คือ 1 ช่องในตาราง • formatSpecifier คือ ตัวกำหนดรูปแบบการแสดงผลของนิพจน์ เช่น ตัวทศนิยม, ตัวเลข เป็นต้น “{index  [,alignment]  [:formatSpecifier]}”

  6. รูปแบบของ formatSpecifier

  7. ตัวอย่าง • using System; • classMainClass •     { • publicstaticvoid Main(string[] args) •         { • Console.WriteLine("x = {0} y = {1}",123 ,456); • Console.WriteLine("123456789"); • Console.WriteLine("{0,9}",123); • Console.WriteLine("123456789"); • Console.WriteLine("{0,-9}",123); • Console.WriteLine("123456789123456789"); • Console.WriteLine("{0,-9}{0,9}", 123,456); • Console.ReadLine(); •         } •     }

  8. จากตัวอย่าง จากโปรแกรมด้านบน มีการใช้งานดังนี้ Console.WriteLine(“x = {0} y = {1}”,123 ,456); หมายถึง คือ กำหนด x = index 0 จะถูกแทนที่ด้วยอาร์กิวเมนต์ตัวแรกที่ชื่อ 123 ดังนั้น จะแสดงผล x = 123 กำหนด y = index 1 จะถูกแทนที่ด้วยอาร์กิวเมนต์ตัวที่สองที่ชื่อ 456 ดังนั้น จะแสดงผล y = 123 Console.WriteLine(“{0,9}”,123) ; หมายถึง Index 0 alignment 9 จะแสดงผล 123 แบบชิดขวาไป 9 ช่อง Console.WriteLine(“{0,-9}”,123) ; หมายถึง Index 0 alignment -9 จะแสดงผล 123 แบบชิดซ้ายไป 9 ช่อง Console.WriteLine(“{0,-9} {1,9}”,123,456); หมายถึง Index 0 alignment -9 จะแสดงผล 123 แบบชิดซ้ายไป 9 ช่อง Index 1 alignment 9 จะแสดงผล 456 แบบชิดขวาไป 9 ช่อง

  9. System.Console.Read() •  เป็นการอ่านค่าอักขระในตำแหน่งถัดไป ในรูปแบบของ integer(int) int c = Console.Read();

  10. System.Console.ReadLine() •  อ่านข้อมูลอยู่ที่บรรทัดถัดไปในรูปแบบของ string <นิยมใช้มากกว่า> string name = Console.ReadLine();

  11. Parse(string); • การรับข้อมูลที่เป็นตัวเลขการรับข้อมูลผ่านเมธอด ReadLineจะเป็นการรับข้อมูลมาในรูปแบบของ string ดังนั้นถ้าเราต้องการแปลงจาก string ให้เป็นตัวเลขให้ทำดังนี้ • ชนิดข้อมูล.Parse(string); stringstr = Console.ReadLine(); intx = int.Parse(str); doubles = double.Parse(str);

  12. Convert.To< ชนิดข้อมูลที่ต้องการ> (string); ชุดคำสั่งที่ใช้ในการแปลงข้อมูลไปเป็นชนิดที่ต้องการ

More Related