1 / 34

คณิตศาสตร์ และการจัดรูปแบบ

คณิตศาสตร์ และการจัดรูปแบบ. Nerissa Onkhum. Outline. คลาส Math การรับข้อมูลจากผู้ใช้ การจัดรูปแบบ. คลาส Math. การคำนวณ. ง่าย + - * / % ซับซ้อน คลาส Math. เมธอดในคลาส Math. sqrt () ใช้ในการหาค่ารากที่สอง Math . sqrt ( 9 ) จะมีค่าเท่ากับ 3 pow () ใช้ในการหาค่ายกกำลัง

Télécharger la présentation

คณิตศาสตร์ และการจัดรูปแบบ

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. คณิตศาสตร์ และการจัดรูปแบบ Nerissa Onkhum

  2. Outline • คลาส Math • การรับข้อมูลจากผู้ใช้ • การจัดรูปแบบ

  3. คลาส Math

  4. การคำนวณ • ง่าย • + - * / % • ซับซ้อน • คลาส Math

  5. เมธอดในคลาส Math • sqrt() ใช้ในการหาค่ารากที่สอง • Math.sqrt(9) จะมีค่าเท่ากับ 3 • pow() ใช้ในการหาค่ายกกำลัง • Math.pow(2, 4) จะเท่ากับ 24 หรือ 16 นั่นเอง • abs() ใช้ในการหาค่าสัมบูรณ์ • Math.abs(-4) จะเท่ากับ 4

  6. เมธอดในคลาส Math • ceil() ใช้ในการหาค่าเลขจำนวนเต็มน้อยที่สุดที่มากกว่าตัวเลขที่ระบุไว้ • Math.ceil(3.27) จะมีค่าเท่ากับ 4 • Math.ceil(-3.27) จะมีค่าเท่ากับ -3 • floor() ใช้ในการหาค่าเลขจำนวนเต็มที่มากที่สุดที่น้อยกว่าตัวเลขที่ระบุไว้ • Math.floor(3.27) จะมีค่าเท่ากับ 3 • Math.floor(-3.27) จะมีค่าเท่ากับ -4

  7. เมธอดในคลาส Math • round() ใช้ในการปัดเศษทศนิยมโดยจะหาเลขจำนวนเต็มที่ใกล้กับตัวเลขที่ระบุมากที่สุด • Math.round(3.27) จะมีค่าเท่ากับ 3 • Math.round(-3.27) จะมีค่าเท่ากับ -3 • min() ใช้หาค่าตัวเลขที่น้อยที่สุดระหว่างเลขสองตัวตามที่ระบุไว้ • Math.min(3, 5) จะได้ค่าเท่ากับ 3 • max() ใช้หาค่าตัวเลขที่มากที่สุดระหว่างเลขสองตัวตามที่ระบุไว้ • Math.max(3, 5) จะได้ค่าเท่ากับ 5

  8. เมธอดในคลาส Math • toDegrees() แปลงมุม radian เป็นมุม degree • Math.toDegrees(Math.PI)มีค่าเท่ากับ 180.0 • toRadians() แปลงมุม degree เป็นมุม radian • Math.toRadians(180) มีค่าเท่ากับ 3.141592653589793 • sin() • Math.sin(Math.toRadians(90)) มีค่าเท่ากับ 1

  9. ปริมาตรวัตถุทรงกลม • ปริมาตร = (4÷3) ×  × รัศมี3 public class BallVolume { public static void main(String[] args) { double radius = 10; double volume = 4.0 / 3.0 * Math.PI* Math.pow(radius, 3); System.out.println(volume); } }

  10. เลขสุ่ม • เมธอด random() • return ค่าสุ่มในช่วง 0 เกือบถึง 1 โดยไม่รวมเลข 1 • ใช้เมธอด Math.random() • ในครั้งแรกอาจได้ค่าเป็น 0.33456876 ครั้งที่สองอาจได้ค่าเป็น 0.805566743 เป็นต้น

  11. เลขสุ่ม • เมธอด random() • ตัวอย่าง โปรแกรมสร้างเลขสุ่มที่เป็นจำนวนเต็มที่อยู่ในช่วง 0 ถึง 99 เป็นจำนวนสิบตัว for (int i = 0; i < 10; i++){ double r = Math.random() * 100; int rand = (int) r; System.out.println(rand); } 72 98 40 96 37 .. .. สุ่มตัวเลข แปลง double ให้เป็น int

  12. การรับข้อมูลจากผู้ใช้การรับข้อมูลจากผู้ใช้

  13. คลาส java.util.Scanner • คลาส • java.util.Scanner • เริ่มโดยการส่ง System.in เพื่อนำไปสร้างวัตถุ Scanner ก่อน จากนั้นจึงเรียกเมธอดในวัตถุ Scanner เพื่อรอรับข้อมูลต่างๆ • เมธอด • nextInt(); รับจำนวนเต็มที่ผู้ใช้พิมพ์เข้ามา • nextDouble(); รับทศนิยมที่ผู้ใช้พิมพ์เข้ามา • next(); รับ String ที่ผู้ใช้พิมพ์เข้ามา

  14. ตัวอย่าง ส่ง System.in เพื่อนำไปสร้างวัตถุ Scanner public class InputData{ public static void main(String[] args){ java.util.Scanner sc; sc = new java.util.Scanner(System.in); System.out.print("Please enter an integer : "); int i = sc.nextInt(); System.out.print("Please enter a double : "); double d = sc.nextDouble(); System.out.print("Please enter a string : "); String s = sc.next(); System.out.println("Your Integer is " + i); System.out.println("Your Double is " + d); System.out.println("Your String is " + s); } } รับจำนวนเต็ม ที่ผู้ใช้พิมพ์เข้ามา รับจำนวนทศนิยม ที่ผู้ใช้พิมพ์เข้ามา รับข้อความ ที่ผู้ใช้พิมพ์เข้ามา

  15. ผลการทำงาน

  16. ถ้าไม่อยากใช้ชื่อเต็มถ้าไม่อยากใช้ชื่อเต็ม import java.util.Scanner; class MyClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ... } }

  17. การพิมพ์ข้อความด้วยเมธอด println()

  18. การพิมพ์ข้อความด้วยเมธอด println() รูปแบบ ตัวแสดงผล.println(ข้อมูลที่ต้องการแสดง);

  19. ตัวอย่างของเมธอด println() public class Println{ public static void main(String[ ] args){ boolean b = true; inti = 2137865423; double d = 7.0/3.0; String s = “Hello”; System.out.println(b); System.out.println(i); System.out.println(d); System.out.println(s); } } true 2137865423 2.33333333333335 Hello

  20. การจัดรูปแบบ

  21. การจัดรูปแบบด้วยเมธอด printf() รูปแบบ ตัวแสดงผล.printf(“รูปแบบ”, ตัวแปร1, ตัวแปร2, …); รูปแบบ จะเป็นรูปแบบที่ต้องการแสดงผล ซึ่งประกอบไปด้วยข้อความ ปกติและรูปแบบการแสดงผล จะนำหน้าด้วยเครื่องหมาย % เสมอ ตัวแปร เป็นตัวแปรหรือวัตถุที่สอดคล้องกับรูปแบบการแสดงผลที่กำหนด ไว้ใน Format

  22. การจัดรูปแบบด้วยเมธอด printf() • ตัวอย่าง public class Printf { public static void main (String[ ] args) { inti = 3277; double d = 132324.25; System.out.printf("%d\n", i); System.out.printf("%f\n", d); System.out.printf("%16f\n", d); System.out.printf("%16.2f\n", d); System.out.printf("%,16.2f\n", d); } }

  23. การจัดรูปแบบด้วยเมธอด printf() • รูปแบบ • ตัวแสดงผล.printf("รูปแบบ", ตัวแปร1, ตัวแปร2, ...); • \n คือ การขึ้นบรรทัดใหม่ • %d แสดงเลขจำนวนเต็ม ถ้าต้องการชิดขวาเราต้องระบุจำนวนช่องลงไป เช่น %20d คือ การสั่งให้พิมพ์ตัวเลขชิดขวาในพื้นที่ 20 ช่อง • %x แสดงเลขจำนวนเต็มฐาน 16 • %f แสดงเลขทศนิยม • %,20.2f แสดงเลขมีจุลภาคและจุดทศนิยมสองตำแหน่ง ให้ชิดขวาความกว้างรวม 20 ช่อง

  24. การจัดรูปแบบด้วยคลาสในกลุ่ม Format • คลาส NumberFormat • getCurrencyInstance() รูปแบบค่าเงิน • getIntegerInstance() รูปแบบเลขจำนวนเต็ม • getNumberInstance() รูปแบบเลขทศนิยม • getPercentInstance() รูปแบบเปอร์เซ็นต์

  25. การจัดรูปแบบด้วยคลาสในกลุ่ม Format import java.text.NumberFormat; public class NumberFormatting{ public static void main (String[] args){ doubled = 37625.72558; NumberFormat nf; nf = NumberFormat.getCurrencyInstance(); System.out.println(nf.format(d)); nf = NumberFormat.getIntegerInstance(); System.out.println(nf.format(d)); nf = NumberFormat.getNumberInstance(); System.out.println(nf.format(d)); nf = NumberFormat.getPercentInstance(); System.out.println(nf.format(d)); } } ß37,625.73 37,626 37,625.726 3,762,573%

  26. การจัดรูปแบบด้วยคลาสในกลุ่ม Format • รูปแบบการแสดงผล • วัถตุที่ได้จากเมธอด getCurrencyInstance() จะได้ข้อความที่ขึ้นต้นด้วยหน่วยเงิน มีจุลภาคและจุดทศนิยม • วัถตุที่ได้จากเมธอด getIntegerInstance() จะได้ข้อความที่มีจุลภาค และเป็นเลขจำนวนเต็ม • วัตถุที่ได้จากเมธอด getNumberInstance() จะได้ข้อความที่มีจุลภาคและจุดทศนิยมสามตำแหน่ง • วัตถุที่ได้จากเมธอด getPercentInstance() จะได้ข้อความที่เป็นตัวเลขคูณด้วย 100 มีจุลภาคและ % ต่อท้าย

  27. การจัดรูปแบบตัวเลขตามต้องการการจัดรูปแบบตัวเลขตามต้องการ • คลาส DecimalFormat จัดรูปแบบการแสดงผลของตัวเลข ตัวอย่างรูปแบบการแสดงผล

  28. การจัดรูปแบบตัวเลขตามต้องการการจัดรูปแบบตัวเลขตามต้องการ import java.text.DecimalFormat; public class DecimalFormatting1{ public static void main (String[] args){ double d = 37625.72558; DecimalFormat df; df = new DecimalFormat("0.00"); System.out.println(df.format(d)); df = new DecimalFormat("#,##0.00"); System.out.println(df.format(d)); df = new DecimalFormat("-#,##0.00"); System.out.println(df.format(d)); df = new DecimalFormat("(#,##0.00)"); System.out.println(df.format(d)); } } 37625.73 37,625.73 -37,625.73 (37,625.73)

  29. การจัดรูปแบบวันที่ • ตัวอย่างความละเอียดการแสดงผลวันที่

  30. การจัดรูปแบบตัวเลขตามต้องการการจัดรูปแบบตัวเลขตามต้องการ 2 ก.ค. 2554 2/07/2554 2 ก.ค. 2554 2 กรกฎาคม 2554 วันอังคารที่ 2 กรกฎาคม 2554 import java.text.DateFormat; import java.util.Date; public class DateFormatting{ public static void main (String[] args){ Date d = new Date(); DateFormat df; df = DateFormat.getDateInstance(); System.out.println(df.format(d)); df = DateFormat.getDateInstance(DateFormat.SHORT); System.out.println(df.format(d)); df = DateFormat.getDateInstance(DateFormat.MEDIUM); System.out.println(df.format(d)); df = DateFormat.getDateInstance(DateFormat.LONG); System.out.println(df.format(d)); df = DateFormat.getDateInstance(DateFormat.FULL); System.out.println(df.format(d)); } }

  31. สรุป • คลาสMath มีเมธอดสำหรับการคำนวณทางด้านคณิตศาสตร์ชั้นสูง • เมธอดprintln() ใช้สำหรับแสดงผลลัพธ์ออกทางหน้าจอ • เมธอด printf() ใช้แสดงผลลัพธ์เช่นเดียวกับเมธอด println() แต่สามารถจัดรูปแบบการแสดงผลได้หลากหลาย • คลาส NumberFormatช่วยจัดรูปแบบการแสดงผลได้

  32. แบบทดสอบ • กำหนดตัวเลขดังนี้ 42,20,35,44,48,23 ให้เขียนโปรแกรมหาตัวเลขมากที่สุดในตัวเลขหกตัวนี้ • กำหนดตัวเลขดังนี้ 4,2,3,5,1,0 ให้เขียนโปรแกรมหาตัวเลขน้อยที่สุดในตัวเลขหกตัวนี้ • เขียนโปรแกรมทอยลูกเต๋า โดยผลลัพธ์จะเป็นตัวเลขจำนวนเต็มหนึ่งตัวที่มีค่าอยู่ระหว่าง 1 ถึง 6 • เขียนโปรแกรมสุ่มตัวเลข ให้ได้ผลลัพธ์เป็นเลขคี่ที่มีค่าในช่วง 0-10

  33. แบบทดสอบ • จงเขียนโปรแกรม yearDate.java ที่อ่านข้อมูลจาก keyboard ที่เป็นอายุของผู้ใช้ในรูปแบบของปี ให้โปรแกรมเปลี่ยนเป็นจำนวนวัน ส่งผลลัพธ์ออกทางหน้าจอ (กำหนดให้ 1 ปีเท่ากับ 365 วัน) • จงเขียนโปรแกรมคำนวณหาพื้นที่สามเหลี่ยม โดยให้รับค่าความสูง และฐาน จากผู้ใช้ สูตรพื้นที่สามเหลี่ยม = ½ × ความสูง × ฐาน

More Related