1 / 11

3 Java Basic Operator ตัวกระทำการ (operator) ในภาษาจาวาโดยแบ่ง ตามประเภทของข้อมูล หรือตัวแปร

3 Java Basic Operator ตัวกระทำการ (operator) ในภาษาจาวาโดยแบ่ง ตามประเภทของข้อมูล หรือตัวแปร. องค์ประกอบตัวดำเนินการ. 1 Numeric Operators 2 Character Operators 3 String Operators 4 Boolean Operators 4.1 Comparison Operators 4.2 Boolean Operators 5 See Also 6 Referrence.

Download Presentation

3 Java Basic Operator ตัวกระทำการ (operator) ในภาษาจาวาโดยแบ่ง ตามประเภทของข้อมูล หรือตัวแปร

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. 3 Java Basic Operator ตัวกระทำการ(operator)ในภาษาจาวาโดยแบ่งตามประเภทของข้อมูล หรือตัวแปร

  2. องค์ประกอบตัวดำเนินการองค์ประกอบตัวดำเนินการ • 1 Numeric Operators • 2 Character Operators • 3 String Operators • 4 Boolean Operators • 4.1 Comparison Operators • 4.2 Boolean Operators • 5 See Also • 6 Referrence

  3. Numeric Operators สัญลักษณ์ที่ใช้กับตัวแปรประเภทตัวเลข มีสัญลักษณ์ดังนี้

  4. ตัวอย่างการใช้งาน ถ้าต้องการแปลง จำนวนชั่วโมง ให้เป็น จำนวนวัน และ ชั่วโมง เช่น กำหนดให้ 27 ชั่วโมง มีค่าเป็นกี่วันกี่ชั่วโมง คำตอบคือ 1 วัน 3 ชั่วโมง จะเขียนโปรแกรมได้ดังนี้ แนวคิดข้อนี้คือ • คิดจำนวนชั่วโมงในหนึ่งวัน และจำนวนชั่วโมงที่เหลือ • พิจารณาตัวแปรเพื่อมาเก็บ • คิดการแปลงหน่วยจากชั่วโมงเป็นวัน มีค่าเป็น 1 วัน มี 24 ชั่วโมง • นำจำนวนชั่วโมงนั้นมาหารด้วย 24 เพื่อหาจำนวนวัน • คิดจำนวนชั่วโมงเศษเหลือจากที่เป็นวัน

  5. เขียนโปรแกรมได้ดังนี้ import java.util.Scanner; public class MyApp3{ public static void main(String args[]){ int hours, days, remainder; Scanner input = new Scanner(System.in); System.out.printf ("กรอกค่าจำนวนชั่วโมง: "); hours = input.nextInt(); days = hours/24; // คำนวณหาจำนวนวัน โดย หารจำนวนชั่วโมงด้วย 24 ค่าที่ได้จะเป็นจำนวนเต็มปัดเศษทิ้ง remainder = hours%24; // คำนวณหาชั่วโมงที่เหลือจากที่นับเป็นวันไปแล้ว หรือ มีค่าเท่ากับ hours-days*24 System.out.printf("%d ชั่วโมง มีค่าเป็น %d วัน %d ชั่วโมง %n",hours,days,remainder); } }

  6. Character Operators ตัวแปรประเภทตัวอักขระเป็นตัวแปรที่เก็บตัวเลขแทนอักขระเพื่อแสดง สัญลักษณ์ ตัวอักษรจึงสามารถ + - * / ได้เหมือนตัวแปรประเภทตัวเลขตัวอย่าง ตัวอย่างโปรแกรมข้างล่าง แสดงตัวอย่างการบวกตัวอักษร 'a' อีก 3 ตัว เมื่อรันและคอมไพล์แล้วจะแสดงผลเป็น d public class TestChar { public static void main(String args[]) { char x = 'a'+3; System.out.printf("%c + %d = %c",'a',3,x); } } ผลลัพธ์ a + 3 = d

  7. ตัวอย่างโปรแกรมข้างล่าง แสดงตัวอย่างหาว่าตัวอักษร 'z' กับ 'a' ต่างกันกี่ตัวอักษร เมื่อรันและคอมไพล์แล้วจะแสดงผลเป็น 25 publicclass TestChar { publicstaticvoid main(String args[]) { int x = 'z'-'a'; System.out.printf("'%c' - '%c' =%d",'z','a',x); } } ผลลัพธ์ 'z' - 'a' = 25

  8. String Operators ตัวแปรประเภทข้อความ เป็นตัวแปรที่ใช้บ่อยอีกตัวแปรหนึ่ง เครื่องหมายที่ใช้ได้กับ String คือ + เท่านั้น ส่วนการกระทำการอื่นๆกับ String ตัวอย่างการใช้งาน ตัวอย่างโปรแกรมข้างล่าง แสดงตัวอย่างการบวกข้อความสองข้อความเข้าด้วยกัน public class TestStringOperator { public static void main(String args[]) { String str1 = "Hello"; String str2 = "UBU"; String str3 = "Computer Science"; String str4 = str1+" "+str3; String str5 = str1+" "+str2; System.out.printf("%s %n %s",str4,str5); } }

  9. ตัวอย่างการบวกข้อความกับตัวแปรชนิดอื่นๆ public class TestStringOperator { public static void main(String args[]) { String str1 = "Peter's ID is "; int year = 52; int faculty = 11; itn facultyRunNo= 333222; String str2 = str1+year+faculty+facultyRunNo; System.out.printf("%s ",str2); } } ผลลัพธ์ที่ได้คือ Peter's ID is 5211333222

  10. Boolean Operators เป็นประเภทตัวแปรเชิงตรรกะซึ่งมีค่าเพียงสองค่าคือtrue และ falseโดยแบ่งออกเป็นสองส่วนคือ ตัวกระทำการสำหรับเปรียบเทียบ และ ตัวกระทำการสำหรับค่าตรรกกะ Comparison Operators เป็นตัวกระทำการที่ใช้สำหรับการเปรียบเทียบค่าประเภทตัวเลข ผลของการเปรียบเทียบจะเป็น boolean (มีค่าเป็น trueหรือ false) ตัวกระทำการสำหรับเปรียบเทียบค่ามีดังนี้

  11. การบ้าน 1. จงเขียนโปรแกรมหาค่าด้านล่าง 2. จงเขียนโปรแกรมหาค่าลงในตารางด้านล่าง

More Related