1 / 72

คณิตศาสตร์ ตัวห่อหุ้ม และการจัดรูปแบบ ( Math, Wrapper and Formatting)

คณิตศาสตร์ ตัวห่อหุ้ม และการจัดรูปแบบ ( Math, Wrapper and Formatting). Nerissa Onkhum. Outline. คลาส Math การรับข้อมูลจากผู้ใช้ ตัวห่อหุ้ม ( Wrappers) การแปลงชนิดข้อมูล (Type Conversions) การจัดรูปแบบ. คลาส Math. การคำนวณ. ง่าย + - * / % ซับซ้อน คลาส Math. เมธอดในคลาส Math.

erin-larson
Download Presentation

คณิตศาสตร์ ตัวห่อหุ้ม และการจัดรูปแบบ ( Math, Wrapper and Formatting)

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. คณิตศาสตร์ตัวห่อหุ้ม และการจัดรูปแบบ(Math, Wrapper and Formatting) Nerissa Onkhum

  2. Outline • คลาส Math • การรับข้อมูลจากผู้ใช้ • ตัวห่อหุ้ม (Wrappers) • การแปลงชนิดข้อมูล (Type Conversions) • การจัดรูปแบบ

  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() ใช้ในการปัดเศษทศนิยมโดยจะหาเลขจำนวนเต็มที่ใกล้กับตัวเลขที่ระบุมากที่สุด (วิเคราะห์ ตัวเลข ถ้า >= .5 ปัดขึ้น < .5 ปัดลง) • 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 เพื่อรอรับข้อมูลต่าง ๆ

  14. เมธอดในคลาส Scanner

  15. ตัวอย่าง ส่ง 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); } } รับจำนวนเต็ม ที่ผู้ใช้พิมพ์เข้ามา รับจำนวนทศนิยม ที่ผู้ใช้พิมพ์เข้ามา รับข้อความ ที่ผู้ใช้พิมพ์เข้ามา

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

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

  18. ทดสอบโปรแกรมรับข้อมูล โดยใช้คลาส Scanner import java.util.Scanner; //เรียกใช้คลาส Scanner public class calnum { public static void main(String[] args){ intx,y; //ประกาศตัวแปร x และ y สำหรับเก็บเลขจำนวนเต็ม Scanner in = new Scanner(System.in); //สร้างออบเจ็กต์ in สำหรับรับข้อมูล System.out.print("Input Number 1 : "); x = in.nextInt(); //รับตัวเลขจำนวนเต็มมาเก็บในตัวแปร x System.out.print("Input Number 2 : "); y = in.nextInt(); //รับตัวเลขจำนวนเต็มมาเก็บในตัวแปร y System.out.println(x + " + " + y + " = " + (x+y) ); //แสดงผลการบวก } }

  19. ปัญหาในการรับข้อมูล import java.util.Scanner; public class InputData { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.print("Please enter a number : "); String s = sc.next(); System.out.print("Your number plus 10 equals "); System.out.println(s + 10); } }

  20. ปัญหาในการรับข้อมูล • ข้อมูลที่รับเข้ามาจากไฟล์ จากฐานข้อมูล จากผู้ใช้ • ปัญหาคือไม่สามารถนำข้อมูลซึ่งเป็นชนิดข้อความมาคำนวณได้ มักอยู่ในรูปของข้อความ

  21. ตัวห่อหุ้ม (Wrappers)

  22. ตัวห่อหุ้ม • จาวาสร้างกลุ่มของคลาสที่ช่วยในการแปลงจากข้อความเป็นข้อมูลชนิดพื้นฐานแต่ละแบบ หรือแปลงจากชนิดข้อมูลพื้นฐานแต่ละแบบไปเป็นข้อความได้ • กลุ่มของคลาสที่ช่วยในการแปลงชนิดข้อมูลเหล่านี้ว่า Wrapper • แปลง String เป็นชนิดข้อมูลพื้นฐาน • แปลงจากชนิดข้อมูลพื้นฐานเป็น String • มีค่าคงที่ของค่าที่มากสุดและน้อยของชนิดข้อมูลพื้นฐาน

  23. คลาสที่เป็นตัวห่อหุ้มคลาสที่เป็นตัวห่อหุ้ม

  24. การสร้างวัตถุ Wrapper Boolean bo = new Boolean(true); Boolean bo = new Boolean("true"); Character c = new Character('c'); Byte by = new Byte((byte)20); Byte by = new Byte("20"); Short s = new Short((short)20); Short s = new Short("20"); เขียนโปรแกรม Java เบื้องต้น

  25. การแปลงชนิดข้อมูล Wrapper  byte Wrapper short Wrapper  int Wrapper  float Wrapper  double • การแปลงชนิดข้อมูล มีวิธีการแปลงดังนี้ • 1. การแปลง wrapper เป็นชนิดข้อมูลพื้นฐาน • 2. การแปลง wrapper เป็นชนิดข้อมูลอ้างอิง (wrapper  String) • 3. การแปลงสตริงเป็นชนิดข้อมูลพื้นฐาน • 4. การแปลงชนิดข้อมูลพื้นฐานเป็นสตริง • 5. การแปลงชนิดข้อมูลพื้นฐาน จากข้อมูลขนาดเล็กไปหาข้อมูลขนาดใหญ่ • 6. การแปลงชนิดข้อมูลพื้นฐาน จากข้อมูลขนาดใหญ่ไปหาข้อมูลขนาดเล็ก ใหญ่ เล็ก ใหญ่ เล็ก

  26. การแปลงชนิดข้อมูล 1. การแปลง Wrapper กลับเป็นชนิดข้อมูลพื้นฐานวิธีที่ 1 ตัวแปรชนิดข้อมูลพื้นฐาน =ตัวห่อหุ้ม.ชนิดข้อมูลพื้นฐานValue(); ตัวอย่าง • String s = “20”; • Integer i = new Integer(s); • int number = i.intValue(); คำอธิบาย บรรทัดที่ 1 ประกาศและกำหนดค่า เรฟเฟอร์เรนซ์sเป็นชนิดคลาส Stringชี้ไปยังวัตถุที่มีค่าเท่ากับ 20 บรรทัดที่ 2 การสร้างวัตถุ iเป็นชนิดคลาส Integer ซึ่งเป็น Wrapper Classes ของ int บรรทัดที่ 3 การนำวัตถุ i (วัตถุของ Wrapper) มาแปลงเป็นชนิด intโดยการใช้ intValue(); Convert Integer to numeric primitive data types byteValue shortValue intValue longValue floatValue doubleValue

  27. การแปลงชนิดข้อมูล • ตัวอย่าง การแปลง Wrapper กลับเป็นชนิดข้อมูลพื้นฐาน publicclass IntegerToNumericPrimitiveTypesExample { publicstaticvoid main(String[] args) {     Integer intObj = new Integer("10"); //use byteValue method of Integer class to convert it into byte type. byte b = intObj.byteValue();     System.out.println(b); //use shortValue method of Integer class to convert it into short type. short s = intObj.shortValue();     System.out.println(s); //use intValue method of Integer class to convert it into int type. int i = intObj.intValue();     System.out.println(i); //use intValue method of Integer class to convert it into long type. long l = intObj.longValue(); System.out.println(l); //use floatValue method of Integer class to convert it into float type. float f = intObj.floatValue();     System.out.println(f); //use doubleValue method of Integer class to convert it into double type. double d = intObj.doubleValue();     System.out.println(d);   } } แปลง wrapper เป็น byte แปลง wrapper เป็น short แปลง wrapper เป็น int แปลง wrapper เป็น long แปลง wrapper เป็น float แปลง wrapper เป็น double

  28. ตัวอย่าง Scanner sc = new Scanner(System.in); System.out.print("Please enter a number : "); String s = sc.next(); Integer i = new Integer(s); int number = i.intValue(); System.out.print("Your number plus 10 equals "); System.out.println(number + 10); เขียนโปรแกรม Java เบื้องต้น

  29. การแปลงชนิดข้อมูล 1. การแปลง Wrapper กลับเป็นชนิดข้อมูลพื้นฐานวิธีที่ 2 ตัวแปรชนิดข้อมูลพื้นฐาน =ตัวห่อหุ้ม.ชนิดข้อมูลพื้นฐานValue(); ตัวอย่าง • String s = “20”; • Integer i = new Integer(s); • int number = i คำอธิบาย บรรทัดที่ 1 ประกาศและกำหนดค่า เรฟเฟอร์เรนซ์sเป็นชนิดคลาส Stringชี้ไปยังวัตถุที่มีค่าเท่ากับ 20 บรรทัดที่ 2 การสร้างวัตถุ iเป็นชนิดคลาส Integer ซึ่งเป็น Wrapper Classes ของ int บรรทัดที่ 3 การนำวัตถุ i (วัตถุของ Wrapper) มาแปลงเป็นชนิด int Convert Integer to numeric primitive data types การนำวัตถุ Wrapper มาแปลงเป็นชนิดข้อมูลพื้นฐานโดยอัตโนมัติ ด้วยกลไลautounboxing

  30. ตัวอย่าง Scanner sc = new Scanner(System.in); System.out.print("Please enter a number : "); String s = sc.next(); Integer i = new Integer(s); int number = i; System.out.print("Your number plus 10 equals "); System.out.println(number + 10); การนำวัตถุ Wrapper มาแปลงเป็นชนิดข้อมูลพื้นฐานโดยอัตโนมัติ ด้วยกลไลautounboxing เขียนโปรแกรม Java เบื้องต้น

  31. การแปลงชนิดข้อมูล • 2. การแปลง Wrapper เป็นสตริง เรฟเฟอร์เรนซ์สตริง =ตัวห่อหุ้ม.toString(); • ตัวอย่าง 1. intnumber = 20; 2. Integer i = new Integer(number); 3. String s = i.toString(); คำอธิบาย บรรทัดที่ 1 ประกาศและกำหนดค่า ตัวแปร iเป็นชนิดข้อมูลพื้นฐาน intมีค่าเท่ากับ 20 บรรทัดที่ 2 การสร้างวัตถุ iเป็นชนิดคลาส Integer ซึ่งเป็น Wrapper Classes ของ int บรรทัดที่ 3 การนำวัตถุ i (วัตถุของ Wrapper) มาแปลงเป็นชนิดคลาส String และไปกำหนดให้กับ เรฟเฟอร์เรนซ์s โดยการใช้ toString(); การนำวัตถุ Wrapper มาแปลงเป็นชนิดข้อมูลเรฟเฟอร์เรนซ์ นั่นคือ แปลงจาก Wrapper เป็น คลาส String โดยการใช้ toString() เขียนโปรแกรม Java เบื้องต้น

  32. การแปลงชนิดข้อมูล • 3. การแปลงสตริงเป็นชนิดข้อมูลพื้นฐาน ตัวแปรชนิดข้อมูลพื้นฐาน =คลาสห่อหุ้ม.parseชนิดข้อมูลพื้นฐาน(); • ตัวอย่างการแปลง String -> int • String str = "20"; • int number = Integer.parseInt(str); คำอธิบาย บรรทัดที่ 1 ประกาศและกำหนดค่า เรฟเฟอร์เรนซ์strเป็นชนิดคลาส Stringชี้ไปยังวัตถุที่มีค่าเท่ากับ 20 บรรทัดที่ 2 การแปลงจาก String ไปเป็น intโดยการใช้ Integer.parseInt(str); byte number = Byte.parseByte(str); //การแปลง String เป็น byte short number = Short.parseShort(str); //การแปลง String เป็น short long number = Long.parseLong(str); // การแปลง String เป็น long float number = Float.parseFloat(str); // การแปลง String เป็น float double number = Double.parseDouble(str); //การแปลง String เป็น double เขียนโปรแกรม Java เบื้องต้น

  33. การแปลงสตริงเป็นชนิดข้อมูลพื้นฐานการแปลงสตริงเป็นชนิดข้อมูลพื้นฐาน • ตัวอย่าง Scanner sc = new Scanner(System.in); System.out.print("Please enter a number : "); int number = Integer.parseInt(sc.next()); เขียนโปรแกรม Java เบื้องต้น

  34. การแปลงชนิดข้อมูล • 4. การแปลงชนิดข้อมูลพื้นฐานเป็นสตริง เรฟเฟอร์เรนซ์สตริง =คลาสห่อหุ้ม.toString(ข้อมูลพื้นฐาน); • ตัวอย่างการแปลง intเป็น String 1. inti = 10; • String s = Integer.toString(i); บรรทัดที่ 1 ประกาศและกำหนดค่า ตัวแปร i เป็นชนิดข้อมูลพื้นฐาน intมีค่าเท่ากับ 10 บรรทัดที่ 2 การแปลงจาก intไปเป็น String โดยการใช้ Integer.toString(i); การแปลงจาก byte เป็น String byte i = 10; String s = Byte.toString(i); การแปลงจาก short เป็น String short i = 10; String s = Short.toString(i); การแปลงจาก long เป็น String long i = 10L; String s = Long.toString(i); การแปลงจาก float เป็น String float i = 10.0f; String s = Float.toString(i); การแปลงจาก double เป็น String double i = 10.0; String s = Double.toString(i); เขียนโปรแกรม Java เบื้องต้น

  35. การแปลงชนิดข้อมูลพื้นฐานเป็นสตริงการแปลงชนิดข้อมูลพื้นฐานเป็นสตริง • ตัวอย่าง Scanner sc = new Scanner(System.in); System.out.print("Please enter a number : "); String s = Integer.toString(sc.nextInt()); เขียนโปรแกรม Java เบื้องต้น

  36. ค่าคงที่ในคลาส Wrapper • Integer • Integer.MIN_VALUE = -2147483648 • Integer.MAX_VALUE = 2147483647 • Double • Double.MIN_VALUE = 4.9E-324 • Double.MAX_VALUE = 1.7976931348623157E308 เขียนโปรแกรม Java เบื้องต้น

  37. หลักการการแปลงชนิดข้อมูลหลักการการแปลงชนิดข้อมูล • ชนิดข้อมูลตัวเลขจำนวนเต็มสามารถแปลงให้เป็นชนิดข้อมูลตัวเลขทศนิยมได้ • ชนิดข้อมูล float สามารถแปลงให้เป็นชนิดข้อมูล double ได้ • ชนิดข้อมูลตัวเลขจำนวนเต็มมีขนาดเรียงกันจากน้อยไปมากดังนี้ byte  short  int  long • ชนิดข้อมูล char สามารถแปลงให้เป็นชนิดข้อมูล int ได้ • ชนิดข้อมูล boolean จะไม่มีความสัมพันธ์กับชนิดข้อมูลแบบพื้นฐานอื่นๆ

  38. การแปลงชนิดข้อมูล • ในกรณีที่ตัวถูกดำเนินการทั้งสองมีชนิดข้อมูลที่ต่างกัน ภาษาจาวาจะมีหลักการแปลงชนิดข้อมูล (type conversion) ดังนี้ • ถ้าตัวถูกดำเนินการตัวหนึ่งมีชนิดข้อมูลเป็น double ตัวถูกดำเนินการอีกตัวหนึ่งจะถูกแปลงให้มีชนิดข้อมูลเป็น double โดยอัตโนมัติ • ถ้าตัวถูกดำเนินการทั้งสองไม่ได้มีชนิดข้อมูลเป็น double แต่มีตัวถูกดำเนินการตัวหนึ่งที่มีชนิดข้อมูลเป็น float ตัวถูกดำเนินการอีกตัวหนึ่งจะถูกแปลงให้มีชนิดข้อมูลเป็น float โดยอัตโนมัติ • ถ้าตัวถูกดำเนินการทั้งสองไม่ได้มีชนิดข้อมูลเป็น double หรือ float แต่มีตัวถูกดำเนินการตัวหนึ่งที่มีชนิดข้อมูลเป็น long ตัวถูกดำเนินการอีกตัวหนึ่งจะถูกแปลงให้มีชนิดข้อมูลเป็น long โดยอัตโนมัติ

  39. การแปลงชนิดข้อมูล • 5. การแปลงชนิดข้อมูลพื้นฐานจากข้อมูลขนาดเล็กไปหาข้อมูลขนาดใหญ่ - การแปลงข้อมูลที่กว้างขึ้น (widening conversion) - ภาษาจาวาจะปรับชนิดข้อมูลให้อัตโนมัติ - สามารถกำหนดค่าชนิดข้อมูลขนาดเล็ก ให้กับชนิดข้อมูลขนาดใหญ่ได้เลย • ตัวอย่าง 1. inti = 4; 2. long l = i; นิพจน์iจะถูกปรับชนิดข้อมูลจาก intให้เป็น long โดยอัตโนมัติ บรรทัดที่ 1 ประกาศและกำหนดค่า ตัวแปร i เป็นชนิดข้อมูลพื้นฐาน intมีค่าเท่ากับ 4 บรรทัดที่ 2 การแปลงจาก intไปเป็น long โดยการกำหนดค่าได้เลย ใหญ่ เล็ก เขียนโปรแกรม Java เบื้องต้น

  40. ตัวอย่างของการแปลงชนิดข้อมูลตัวอย่างของการแปลงชนิดข้อมูล ภาษาจาวาจะปรับชนิดข้อมูลให้อัตโนมัติ ในกรณีต่อไปนี้ • กำหนดค่าชนิดข้อมูลที่เล็กกว่าให้กับตัวแปรชนิดข้อมูลที่ใหญ่กว่าอาทิเช่น int i = 4; long l = i; นิพจน์ i จะถูกปรับชนิดข้อมูลจาก int ให้เป็น long โดยอัตโนมัติ • กำหนดค่าชนิดข้อมูลจำนวนเต็มให้กับจำนวนเลขทศนิยมอาทิเช่น • double x = 3; นิพจน์ 3 จะถูกปรับชนิดข้อมูลจาก int ให้เป็น double โดยอัตโนมัติ

  41. การแปลงชนิดข้อมูล • 6. การแปลงชนิดข้อมูลพื้นฐานจากข้อมูลขนาดใหญ่ไปหาข้อมูลขนาดเล็ก • การแปลงข้อมูลที่แคบลง (narrowing conversion) ซึ่งอาจมีผลให้เสียความละเอียดของข้อมูลบางส่วนไป ชนิดข้อมูลตัวแปร = (ชนิดข้อมูลที่ต้องการเปลี่ยน)expression; • ตัวอย่าง int amount = (int) 3.0; • แปลงนิพจน์ที่มีค่า 3.0 ข้อมูลชนิด double ให้เป็นชนิด int ใหญ่ เล็ก เขียนโปรแกรม Java เบื้องต้น

  42. ตัวอย่างของการแปลงชนิดข้อมูลตัวอย่างของการแปลงชนิดข้อมูล • ตัวอย่างที่ไม่ถูกต้อง int amount = 123L; //illegal float f = 4.0; //illegal

  43. ตัวอย่างโปรแกรม public class PromotionDemo { public static void main(String args[]) { inti; long l; float fl = 4.2f; i = 4; l = i; fl = i; double x = fl; fl = 4.2; //illegal } }

  44. ตัวอย่างของ Typecasting • ตัวอย่าง int amount = (int) 3.0; • แปลงนิพจน์ที่มีค่า 3.0 ข้อมูลชนิด double ให้เป็นชนิด int • ตัวอย่างที่มีการตัดข้อมูลออก int x; double y = 1.25; x = (int) y;

  45. ตัวอย่างโปรแกรม public class TypecastingDemo { public static void main(String args[]) { int b1 = 4; int b2 = 3; byte b3; b3 = (byte)(b1+b2); float fl; fl = (float)3.2; } }

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

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

  48. ตัวอย่างของเมธอด 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

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

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

More Related