Java Programming Language

Download Report

Transcript Java Programming Language

การเขียนโปรแกรมเชิงวัตถุด้วย
JAVA & NetBean6.5
อ. นัฐพงศ์ ส่ งเนียม
[email protected]
http://www.siam2dev.com
วิทยาการคอมพิวเตอร์ คณะวิทยาศาสตร์และเทคโนโลยี
มหาวิทยาลัยราชภัฏพระนคร
Lec02 : introduction to java
Agenda : เนือ้ หาในบทนีป้ ระกอบไปด้ วย
Identifiers , Literals , Lexical , Token
 Keywords
 and Types, Primitive Types
 Control Structure in java

Objectives : วัตถุประสงค์






Use comments in source program (เรี ยนรู้การใช้งาน คอมเมนต์)
Distinguish between valid and invalid identifiers (สามารถบอก
ความแตกต่างระหว่าง การตั้งชื่อที่ถูกต้องและไม่ถูกต้อง)
Recognize Java technology keywords ( รู้จกั กับ คา ของภาษาจา)
List the eight primitive types (รู ้จกั กับข้อมูลชนิดพื้นฐานทั้ง 8 ชนิด)
Define literal values for nummeric and textual types
Define the terms primitive variable and reference
variable (รู ปแบบของชนิดข้อมูลพื้นฐาน และข้อมูล ประเภท reference)
Objectives : วัตถุประสงค์





Declare variables of class type (วิธีการประกาศ class )
Construct an object using new (การสร้าง Object ด้วย
คาสั่ง new )
Describe default initialization (อธิบายถึง default
initialization)
Describe the significance of reference variable
State the consequences of assigning variables of
class type
Comment

มี 3 วิธีที่จาวา ยอมให้ใช้ Comment ได้ คือ
// comment on one line
/* comment on one
or more lines */
/** documentation comment */
ตัวอย่ างการใช้ Comment ในโปรแกรม
// TestComment.java
/* Developer : Nattapong Songneam
ID :: 501122294123
Developed Date : 31/07/2006
Last Update : 31/07/2006
*/
class TestComment
{
public static void main(String args[])
{
// this comment one line
/* this is a comment more line
System.out.println(“this program for test Comment”);
} // end main()
} // end class
ต.ย. Comment
เพือ่ อธิบาย รายเอียดเกีย่ วกับ
โปรแกรม ว่ าผู้พฒ
ั นา ชื่ออะไร
พัฒนาขึน้ เมือ่ ใด และแก้ ไขล่ าสุ ดเมือ่ ใด
Semicolons, Blocks , white space
A statement is one or more line of code
terminates by a semicolon (;).
sum = a + b + c
+ d + e + f;
A block is a collection of statement bound by
opening and closing braces
{
x = X + Y;
z = I + J;
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworld;
ภาษาจาวาจบ 1
ประโยคคาสัง่ ด้วย ;
ดังนั้นการขึ้นบรรทัด
ใหม่จึงไม่ผดิ รู ปแบบไว
ยากรณภาษาจาวา
/**
*
* @author nat
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int x, y,z;
int num1;
x=5;
y=10;
z= x
+
y;
double d = 1;
System.out.println("สวัสดีชาวโลก ..." + z);
System.out.println("Double... " + d);
}
}
Semicolons, Blocks , white space

You can use a block in a class defination :
public class MyDate {
private int day;
private int mouth;
private int year;
}


You can nest block statements.
Any amount of white space is allowed in java
program.
Identifiers : ชื่อ , และการตั้งชื่อ






Identifiers คือการตั้งชื่อ ให้กบั ตัวแปร คลาส หรื อ เมธอด
การตั้งชื่อสามารถขึ้นต้นด้วย Unicode , underscore(_) หรื อ
dollar sign ($)
ภาษาจาวาเป็ นภาษา case-sensitive ตัวพิมพ์เล็กใหญ่มีความหมาย
แตกต่างกัน
ไม่จากัดจานวนตัวอักษร (no maximum length)
ห้ามมีเครื่ องหมายทางคณิ ตศาสตร์ ใน ชื่อตัวแปร เช่น int A+B; ไม่ได้
ห้ามมีช่องว่างใน ชื่อตัวแปร เช่น int A B ;
ตัวอย่างการตั้งชื่อ : Identifier Exp.
ถูกต้อง
ไม่ถูกต้อง
identifier
username
UserName
user_name
_sys_var1
$change
1identifier
user name
x1+5
Y*5
Java Keywords

คาที่มีความหมายในภาษาจาวา ซึ่ งถูกสงวนไว้ให้จาวาใช้งาน เช่น



abstract , boolean , break , byte , case, catch ,
char ,class ,continue , default ,do ,double ,else
,extends ,false ,final , finally , float , for, if ,
implements , import , instantceof ,int , interface ,
long ,native ,new ,null , package , private ,
protected , public , return , short ,static , strictfp ,
super , switch , synchronized , this ,throw ,
transient ,true ,try , void , volatile , while
มีท้ งั หมด 50 คา
ห้ามตั้งชื่อตรงกับ Keyword
Primitive Types


Primitive Types คือ ชนิดข้อมูลที่ไม่สามารถแตกย่อย
ออกเป็ นส่ วนประกอบอื่นได้อีก เช่น ตัวเลข ตัวอักษร และค่า
ความจริ งเป็ นต้น
ภาษาจาวากาหนดให้มีขอ้ มูลพื้นฐาน (Primitive
Types)ทั้งหมด 8 ชนิด แบ่งออกเป็ น 4 ประเภทดังนี้คือ




Logical : boolean
Textual : char
Integral : byte ,short , int ,and long
Floating : float and double
Logical : boolean



เป็ นข้อมูลทางตรรกมีค่า 2 ค่า คือ true กับ false
ใช้เนื้อที่ในหน่วยความจาเท่ากับ 1 byte หรื อ 8 bit
ตัวอย่าง
boolean truth = true;
boolean smile =flase;
จาก ต.ย. ข้างบนเป็ นการประกาศตัวแปร truth เป็ น ชนิด
boolean และกาหนดค่าเป็ น true
ตัวอย่างการใช้งาน boolean
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
// TestBoolean.java
class TestBoolean
{
public static void main(String args[])
{
boolean truth= true;
System.out.println(“คาว่า จริ ง ภาษาอังกฤษใช้คาว่า : ” + truth);
truth = false;
}
}
Textual : char and String




char เป็ นชนิดข้อมูลที่ใช้แทนตัวอักขระ Unicode-16
ใช้เนื้อที่ในหน่วยความจาเท่ากับ 2 byte หรื อ 16 bit
ต้องอยูใ่ นเครื่ องหมาย ‘ ’
ตัวอย่าง

‘a’ , ‘b’ , ‘\t’ , ‘\n’
ต.ย. การประกาศ ตัวแปร char
char ch = ‘a’;
ต.ย. การใช้งาน String
System.out.println(“Hello Java”);
System.out.print(“Hello java” + ‘\n’);
System.out.print(“Hello java” + ‘\t’ + “Java”);
Hello java Java
ตัวอย่างการใช้งาน ตัวแปร char
//TestChar.java
class TestChar
{
public static void main(String[] args)
{
char ch = 'A';
char tab = '\t';
System.out.println("Hello World! " + tab + ch);
}
---------- Java Run ---------}
Hello World!
A
Normal Termination
Output completed (0 sec consumed).
Textual : char and String




String : ข้อมูล String ไม่ใช่ขอ้ มูลพื้นฐาน แต่เป็ นข้อมูล
คลาส
String คือ สายอักขระที่เรี ยงต่อกัน ต้องอยูใ่ นเครื่ องหมาย “”
ตัวอย่าง “Hello! World.”
การประกาศใช้งานคลาส String
String Greeting = “Good morning”;
String errMessage = “file not found”;
Integral : byte , short , int , and long

Integral เป็ นข้อมูลประเภทตัวแปรเลขจานวนเต็มประกอบไปด้วย





Use three form : สามารถใช้ได้ 3 รู ปแบบ ดังนี้





byte ใช้เนื้อที่ในหน่วยความจาเท่ากับ 1 byte หรื อ 8 bit
short ใช้เนื้อที่ในหน่วยความจาเท่ากับ 2 byte หรื อ 16 bit
int ใช้เนื้อที่ในหน่วยความจาเท่ากับ 4 byte หรื อ 32 bit
long ใช้เนื้อที่ในหน่วยความจาเท่ากับ 8 byte หรื อ 64 bit
2 : decimal > ฐานสิ บ
077 : octal > ฐานแปด
0XBAAC hexadecimal > ฐานสิ บหก
กรณี ที่ไม่มีตวั อักษรกากับด้านหลังของตัวแปร ค่าปกติคือ int
ถ้าหากต้องการกาหนดให้เป็ น long ต้องใช้ตวั L หรื อ l กากับท้ายชื่อตัวแปรด้วย
ตัวอย่างการใช้งาน ตัวแปร long
// TestIntegral.java
class TestIntegral
{
public static void main(String[] args)
{
long V = 300000000L;
// velocity of light equal to 3 x 100000000 meter per sec.
System.out.println("velocity of light equal to : " + V + "m/s" );
}
}
---------- Java Run ---------velocity of light equal to : 300000000m/s
Normal Termination
Output completed (0 sec consumed).
ตัวอย่างการใช้งาน ตัวแปร int และ long
// TestIntegral.java
class TestIntegral
{
public static void main(String[] args)
{
long V = 300000000L;
// velocity of light equal to 3 x 100000000 meter per sec.
int v1 = 300000;
// velocity of light equal to 3 x 100000 Km per sec.
System.out.println("velocity of light equal to : " + V + " m/s"
", or " + v1 + " Km/s" );
}
}
---------- Java Run ----------
+
velocity of light equal to : 300000000 m/s, or 300000 Km/s
Normal Termination
Output completed (0 sec consumed).
type wraper classes


ในภาษาจาวา มี type wrapper classes ของชนิดข้อมูล
เลขจานวนเต็มคือ Byte , Short , Integer และ
Long คลาสเหล่านี้มีค่าคงที่ ซึ่งกาหนดขนาดสูงสุ ดและขนาด
ต่าสุ ดของชนิดข้อมูลนั้นไว้ นอกจากนี้ยงั บรรจุดเมธอดที่จาเป็ น
สาหรับการทางานกับข้อมูลประเภทเลขจานวนเต็มไว้อีกด้วย เช่น
ต.ย. การแปลง ตัวอักขระให้เป็ นตัวเลข


String ch = “10”;
Int ch1 = Integer.parseInt(ch);
ต.ย. การใช้งาน type wraper classes
1. //IntegralConstants
2. class IntegralConstants
3. {
4.
public static void main(String[] args)
5.
{
6.
System.out.println("Max value of Byte : " + Byte.MAX_VALUE);
7.
System.out.println("Min value of Byte : " + Byte.MIN_VALUE);
8.
System.out.println("Max value of Short : " + Short.MAX_VALUE);
9.
System.out.println("Min value of Short : " + Short.MIN_VALUE);
10.
System.out.println("Max value of Integer : " + Integer.MAX_VALUE);
11.
System.out.println("Min value of Integer : " + Integer.MIN_VALUE);
12.
System.out.println("Max value of Long : " + Long.MAX_VALUE);
13.
System.out.println("Min value of Long : " + Long.MIN_VALUE);
14.
}
15. }
Exercise 1


จงหาระยะทางทั้งหมดที่แสงเดินทางได้เป็ น km ภายในเวลา 1 ปี
(ใน 1 วินาทีแสงเดินทางได้ 300000 km)
จงเขียนโปรแกรมเพื่อรับค่า จานวนปี แสง (Light Year) จาก
keyboard แล้วคานวณหาค่า ระยะทางที่แสงเดินทางตาม
จานวนปี แสงที่รับมา เช่น 2 ปี แสงเท่ากับ
(18,921,600,000,000 km)
1.
2.
3.
4.
5.
6.
7.
8.
9.
// TestVelocity.java
import java.io.*;
class TestVelocity
{
public static void main(String[] args) throws IOException
{
try
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
}
String v = "";
System.out.print("Enter the Light Year : ");
v = stdin.readLine();
Long v1 = Long.parseLong(v);
long v2 = v1*300000L*60L*60L*24L*365L;
System.out.println("distance Of " + v1 + " Light Year is : " + v2 + " km");
}
catch (Exception e)
{
}
}
Screen shot
การกาหนดให้รับค่าจาก Argument





สาหรับ NetBean
v.6.0
ให้ไปที่เมนู Build เลือก Set
main Project
Configuraiton
แล้วเลือก Customize
แล้วจะปรากฏหน้าต่างดังรู ป
ให้ป้อนค่าที่ตอ้ งการลงในช่อง
Argument
Floating : float and double


Float : ข้อมูลประเภทจุดทศนิยม
การประกาศค่าตัวแปร
รู ปแบบ float ชื่ อตัวแปร
ต.ย. float Pi;

การกาหนดค่าให้กบั ตัวแปร
Pi =3.14F;
Pi = 3.14f;

เราสามารถประกาศค่าและกาหนดค่าไปพร้อมกันในประโยคเดียวกันได้
float Pi =3.14f;
ตัวอย่างการใช้งาน Floating Point
// TestFloat_Double.java
2.
class TestFloat_Double
3.
{
4.
public static void main()
5.
{
6.
float Pi=3.14f;
7.
int R = Integer.parseInt(args[0]);
8.
double d = 3.141450141242145d;
9.
System.out.println(“Circle Area is : ” + Pi*R*R);
10.
System.out.println(“circular Line is : ” + 2*d*R);
11. }
12. }
1.
Floating : float and double


Double : ข้อมูลประเภทจุดทศนิยม ขนาด
การประกาศค่า
double Pi;

การกาหนดค่า
Pi = 3.1414;

เราสามารถเขียนให้อยูใ่ นบรรทัดเดียวกันได้ดงั นี้
double Pi = 3.1414;
type wraper classes


ในภาษาจาวา มี type wrapper classes ของชนิดข้อมูลเลขทศนิ
ยคือ Float และ Double คลาสเหล่านี้มีค่าคงที่ ซึ่ งกาหนดขนาดสู งสุ ด
และขนาดต่าสุ ดของชนิดข้อมูลนั้นไว้ นอกจากนี้ยงั บรรจุดเมธอดที่จาเป็ น
สาหรับการทางานกับข้อมูลประเภทเลขจานวนเต็มไว้อีกด้วย เช่น
ต.ย. การแปลง ตัวอักขระให้เป็ นตัวเลข



String ch = “10”;
Float ch1 = Float.parseFloat(ch);
Double ch2 = Double.parseDouble(ch);
ต.ย. การใช้งาน type wraper classes
1.
2.
3.
4.
5.
//IntegralConstants
class IntegralConstants
{
public static void main(String[] args)
{
System.out.println("Max value of float : " + Float.MAX_VALUE);
System.out.println("Min value of float : " + Float.MIN_VALUE);
System.out.println("Max value of double : " + Double.MAX_VALUE);
System.out.println("Min value of double : " + Double.MIN_VALUE);
6.
7.
8.
9.
10.
11.
}
}
Term and Expressions


Term คือค่าหนึ่งค่า ซึ่งอาจจะเป็ นค่าคงที่ ตัวแปร หรื อค่าที่ถูกส่ ง
มาจาก method โดยปกติ term จะปรากฏอยูใ่ นตาแหน่ง
ของ expression หรื อภายใน expression
Expressions คือหน่วยของโปรแกรม
Operators

ภาษาจาวา แบ่ง operators ออกเป็ น 6 กลุ่มคือ
1.
Assignment
2.
Arithmetic
Bitwise
Relational
Logical
Conditional
3.
4.
5.
6.
Operators
1. Assignment Operators

Assignment Operators สาหรับภาษาคอมพิวเตอร์
หลาย ๆ ภาษา มักจะถือว่าเครื่ องหมาย = เป็ นเครื่ องหมายใน
ประโยค assignment แต่ภาษาจาวา (นาความคิดนี้มาจาก
ภาษา c) ถือว่า = เป็ น operator สามารถใช้ภายใน
expression ได้เหมือนกับ operator ทัว่ ไป อย่างเช่น
x + (y =1) จะมีความหมายว่าตัวแปรy ถูกกาหนดค่าด้วย
1 แล้วค่าที่ใช้ในการกาหนดค่านั้นคือ 1 ถูกส่ งออกมาเป็ นผล
ของการกาหนดค่า ซึ่งในกรณี น้ ีจะถูกนาไปบวกกับตัวแปร x
ต.ย. ทดสอบผลลัพธ์ ของการใช้ assignment
operators ในโปรแกรมต่ อไปนี้
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
//testOperators1.java
class testOperators1 {
public static void main(String args[]) {
int x, y, z;
x=y=z=1;
System.out.println(x + “ , ” + y + “ , ” + z);
x=(y=(z=z+1)+1)+1;
System.out.println(x + “ , ” + y + “ , ” + z);
x=(y=1) + (z=1);
System.out.println(x + “ , ” + y + “ , ” + z);
}
}
กาหนดค่า x = y=z=1
ต.ย. ทดสอบผลลัพธ์ ของการใช้ assignment
operators ในโปรแกรมต่ อไปนี้
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
//testOperators1.java
class testOperators1 {
public static void main(String args[]) {
int x, y, z;
x=y=z=1;
System.out.println(x + “ , ” + y + “ , ” + z);
x=(y=(x=z+1)+x)+1;
System.out.println(x + “ , ” + y + “ , ” + z);
x=(y=1) + (z=1);
System.out.println(x + “ , ” + y + “ , ” + z);
}
}
กาหนดค่า x = y=z=1
x=?,y=?,z=?
Operators
2. Arithmetic Operators
Arithmetic Operators

ประเภทดังนี้

เป็ นตัวดาเนินการทางคณิ ตศาสตร์ แบ่งออกเป็ น 4
integer Arithmetic Operators เป็ น Operator ที่ทากับจานวนเต็ม
และได้ผลลัพธ์เป็ นจานวนเต็มเท่านั้น หากผลลัพธ์เป็ นทศนิยมจะถูกเปลี่ยนเป็ น
จานวนเต็ม ได้แก่





+ (บวก)
- (ลบ)
* (คูณ)
/ (หาร)
% (หารเอาเศษ)
ต.ย. การใช้ integer arithmetic
operators
//IntOpt.java
2. class Inopt {
3. public static void main(String arg[]) {
4. System.out.println(10 + 11);
5. System.out.println(11 - 12);
6. System.out.println(13 * 14);
7. System.out.println(14 / 15);
8. System.out.println(15 % 16);
9. }
---------- Java Run ---------21
10. }
-1
1.
?
182
0
15
Output completed (0 sec consumed) - Normal Termination
Exercise 3
1.
2.
3.
จงทดสอบว่าประโยคที่คานวณ 1/0 จะผ่านการคอมไพล์
หรื อไม่
ทดลองกาหนดตัวแปร x= 0 และจากนั้นให้ คานวณ 1/x
จะผ่านการคอมไพล์และ รันหรื อไม่
จงทดสอบว่า (-6 % 7) กับ (-7 % 6) ได้ค่าเท่ากัน
หรื อไม่ ทาไมจึงเป็ นเช่นนั้น
Operators
2. Arithmetic Operators
Arithmetic Operators

ประเภทดังนี้

เป็ นตัวดาเนินการทางคณิ ตศาสตร์ แบ่งออกเป็ น 4
Floating-point Arithmetic Operators เป็ น Operator ที่ทากับ
เลขทศนิยมและได้ผลลัพธ์เป็ นเลขทศนิยมเท่านั้น ได้แก่





+ (บวก)
- (ลบ)
* (คูณ)
/ (หาร)
% (หารเอาเศษ)
สาหรับภาษา C และ C++ สามารใช้ % ได้เฉพาะ
เลขจานวนเต็มเท่านั้น แต่ในภาษาจาวาใช้กบั เลขทศนิยม
ได้ดว้ ย
ต.ย. การใช้ Floating Arithmetic
Operators
//FloatOpt.java
2. class Floatopt {
3. public static void main(String arg[]) {
4. System.out.println(11.0 + 12.00);
5. System.out.println(12.0 - 13.00);
6. System.out.println(14.0 * 15.00);
7. System.out.println(16.0 / 17.00);
8. System.out.println(16.0 % 17.00);
9. }
10. }
---------- Java Run ---------1.
23.0
-1.0
210.0
0.9411764705882353
16.0
Output completed (0 sec consumed) - Normal Termination
Arithmetic Assignment Operators

ภาษา Java นา operators ประเภทนี้มาจากภาษา C นัน่
คือในประโยคกาหนดค่า (assignment) ประโยคหนึ่ง หากมี
ตัวแปรตัวหนึ่ง ปรากฏอยูท่ ้ งั ในด้าน ซ้ายและ ขวา ของ เครื่ องหมาย
กาหนดค่า = ประโยคนั้นอาจเปลี่ยนเป็ นใช้ arithmetic
assignment operators ได้อย่างเช่นประโยค x =
x+1; อาจเปลี่ยนเป็ นประโยค x +=1; โดยมีความหมาย
เหมือนกัน
Arithmetic Assignment Operators

ภาษา Java นา operators มี arithmetic
assignment operators ห้าตัวด้วยกันคือ






+=
-=
*=
/=
%=
โดยใช้ได้ท้ งั ตัวเลขจานวนเต็มและเลขทศนิยม
ต.ย. การใช้งาน
Arithmetic Assignment Operators
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
//AriAssOpt.java
class AriAssOpt {
public static void main(String args[]) {
int x=1;
x +=1; System.out.println(x);
x -=1; System.out.println(x);
x *=1; System.out.println(x);
x /=1; System.out.println(x);
x %=1; System.out.println(x);
float y=1f;
y +=1; System.out.println(y);
y -=1; System.out.println(y);
y *=1; System.out.println(y);
y /=1; System.out.println(y);
y %=1; System.out.println(y);
}
}
Control Statements

Control Statements คือประโยคที่ใช้ในการดาเนินการของโปรแกรมไปใน
ทิศทางที่ตอ้ งการ ที่นอกเหนือไปจากการดาเนินการในลาดับ (sequence ) ของ
ประโยคหรื อคาสัง่ ปกติ แบ่งออกเป็ น









if statement
switch statement
while statement
do statement
for statement
break statement
continue statement
return statement
throw statement
if statement

ประโยค if ใช้ควบคุมโปรแกรมเลือกดาเนินการไปใน
เส้นทางใดเส้นทางหนึ่งระหว่างทางเลือกสองทาง โดยใช้ผล
ของ <boolean expression> เป็ นตัดสิ นใจ
โครงสร้างของประโยค โดยมีรูปแบบของประโยคดังนี้
if (<boolean expression>)
<statement>;
[ else
<statement>;
]
if statement
แบบที่ 2
แบบที่ 1
expression
expression
true
false
Statement 1
Statement 2
false
true
Statement 2
Statement 1
Statement 3
ต.ย. การใช้ประโยค if โดยไม่มี else
//If1.java
2. class If1 {
3. public static void main(String args[]) {
4. int a = Integer.parseInt(args[0]);
5. int b = Integer.parseInt(args[1]);
6. if (b == 0) {
7.
System.out.println(“Error : divide by zero ! ”);
8.
System.exit(0);
9. } // end if แบบไม่มี else
10. System.out.println(“a/b : ” + a/b);
11. }
12. }
1.
ต.ย. การใช้ประโยค if ที่มี else
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
//If2.java
class If2 {
public static void main(String args[]) {
int a = Integer.parseInt(args[0]);
int a = Integer.parseInt(args[1]);
if (b == 0) {
System.out.println(“Error : divide by zero ! ”);
System.exit(0);
} else {
System.out.println(“a/b : ” + a/b);
}
} // end main
} // end class
if กับทางเลือกที่มากว่า หนึ่งทางเลือก
1.
2.
3.
4.
5.


if (c1) {
} else if (c2) {
} else if (c3) {
} else if (cN) {
}
// c เป็ น เงื่อนไข (condition)
// N เป็ น เลขจานวนเต็มใด ๆ
Exercise 2

จงเขียนโปรแกรมเพื่อรับคะแนนทาง จอภาพ แล้วคานวณเป็ นเกรดโดยใช้
เกณฑ์ดงั นี้








ถ้า 80
ถ้า 75
ถ้า 70
ถ้า 65
ถ้า 60
ถ้า 55
ถ้า 50
ถ้า 49
<= score <= 100 ให้ เกรด “A”
<= score <= 79 ให้ เกรด “B+”
<= score <= 74 ให้ เกรด “B”
<= score <= 69 ให้ เกรด “C+”
<= score <= 64 ให้ เกรด “C”
<= score <= 59 ให้ เกรด “D+”
<= score <= 54 ให้ เกรด “D”
< score ให้ เกรด “E”
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
//TestGRADE.java
import java.io.*;
class TestGRADE
{
public static void main(String[] args) throws IOException
{
try
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Your Score : " );
String sc = "";
sc = stdin.readLine();
int sc1 = Integer.parseInt(sc);
String GRADE="";
if (sc1 >= 80)
{
GRADE ="A";
} else if( sc1 >= 75) {
GRADE ="B+";
} else {
GRADE ="E";
}
System.out.println("Your GRADE is : " + GRADE);
}
catch (Exception e)
{
}
28.
29.
30.
} //end main
} // end class
Dangling Else

ภาษา Pascal , C , C++ และภาษาอื่นๆ ที่มีประโยค if ยอมให้มี else เป็ น
option จะมีปัญหาการตีความหมายประโยค if ซึ่ งเรี ยกว่า Dangling Else คือ
ประโยค


if (c1) if (c2) statemet1; else statement2
มี if สองตัวและมี else ตัวเดียว ถือว่าเป็ นประโยคที่ถูกต้องในภาษาที่ประโยค if มี
else เป็ น option แต่ผอู ้ ่านอาจจะตีความประโยคนี้ได้สองแบบคือ แบบแรก อาจ
มองว่า else ตัวนั้นเป็ นของ if ตัวแรก หรื อแบบที่สองอาจมองว่า else นั้นเป็ นของ
if ตัวที่สอง ซึ่ งก็เป็ นไปได้ท้ งั คู่ ดังรู ปต่อไปนี้
if (c1)
if (c2)
statemet1;
else
statement2
if (c1)
if (c2)
statemet1;
else
statement2
Dangling Else (ต่อ)


ภาษาอื่น ๆ อย่าง Algol , Modula-2 , Ada แก้ปัญหานี้โดยกาหนดว่าประโยค
if ต้องจบด้วยการปิ ด end if ก็สามารถมี else เป็ น Option ได้เช่นกัน โดยไม่มี
ปัญหา dangling else
แต่ภาษา Pascal , C , C++ และ Java ไม่ได้แก้ปัญหานี้โดยการเปลี่ยน
โครงสร้างของประโยค if แต่ใช้วธิ ีกาหนดให้ else นั้นเป็ นของ if แรกอยูใ่ กล้ else
มากที่สุด เสมอ เรี ยกว่า The most Recently Nested ดังรู ป
if (c1)
if (c2)
statemet1;
else
statement2
Switch Statements

ประโยค switch ใช้ควบคุมโปรแกรมเลือกดาเนินการไปใน
เส้นทางหนึ่งจากทางเลือกหลายเส้นทาง โดยใช้ค่าของ integer
expression เป็ นตัวตัดสิ นใจ โครงสร้างของประโยค
switch มีดงั นี้
switch (<integer expression>) {
case <value> : <statements> ;
case <value> : <statements> ;
case <value> : <statements> ;
case <value> : <statements> ;
……………………………..
default : <statements>;
}
Switch Statements

ในประโยค switch หนึ่งจะมี case กี่ case ก็ได้ หรื อจะ
ไม่มีเลยก็ได้ อาจจะมี default เป็ น option แสดงว่า
block ของ switch อาจจะว่างเปล่าก็ได้ ดังรู ป
switch (<integer expression>) {
default : <statements>;
}
switch (<integer expression>) {
}
• ค่าของ <integer expression> ต้องไม่ใช่ค่า boolean , float หรื อ double
• ค่า value แต่ละ case ต้องไม่ซ้ ากัน
• ห้ามมี default มากกว่า หนึ่งค่า
ต.ย. การใช้งาน switch
//Switch1.java
class Switch1.java
3. public static void main(String args[]) {
4. char gr = args[0].charAt[0];
5. switch(gr) {
6. case ‘A’ : System.out.println(“Excellent”);
7. case ‘B’ : System.out.println(“Good”);
8. case ‘C’ : System.out.println(“So so”);
9. case ‘D’ : System.out.println(“Fails”);
10. case ‘F’ : System.out.println(“Get lost”);
11. default : System.out.println(“Invalid”);
12. }
13. }
14. }
1.
2.
หาก ป้ อน C เข้ามาจะได้ผลลัพธ์ดงั นี้
---------------------So so
Fails
Get lost
Invalid
ต.ย. การใช้ Break ในการแก้ปัญหา switch
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
//Switch1.java
class Switch1.java
public static void main(String args[]) {
char gr = args[0].charAt[0];
switch(gr) {
case ‘A’ : System.out.println(“Excellent”);
break;
case ‘B’ : System.out.println(“Good”);
break;
case ‘C’ : System.out.println(“So so”);
break;
case ‘D’ : System.out.println(“Fails”);
break;
case ‘F’ : System.out.println(“Get lost”);
break;
default : System.out.println(“Invalid”);
}
}
}
หาก ป้ อน C เข้ามาจะได้ผลลัพธ์ดงั นี้
---------------------So so
While Statements

ประโยค while ใช้ในการควบคุมประโยคที่กาหนดให้ถูกทาซ้ า
จนกว่าเงื่อนไขบางอย่างเป็ นเท็จจึงหยุด โดยโครงสร้างของประโยค
while มีดงั นี้
while (<boolean expression>)
<statement>;
false
expression
true
statements
While Statements
//While1.java
2. class While1 {
3. public static void main(String args[]) {
4. int n = Integer.parseInt(args[0]);
5. int i=1,f=1;
6. while (i++ < n) {
7. f *=i;
8. }
9. System.out.println( n + “! : ” + f);
10. }
11. }
1.
Exercise 3
1.
2.
3.
4.
5.
6.
7.
8.
จงเขียนโปรแกรมเพื่อแสดง เลขคู่ ตั้งแต่ 2,4,6, .. 100
จงเขียนโปรแกรมเพื่อแสดง เลขคี่ ตั้งแต่ 1,3,5, .. 99
จงเขียนโปรแกรมเพื่อแสดง ตัวเลข 99,97,95,93,.. 3,1
จงเขียนโปรแกรมเพื่อแสดง ตัวเลข 100,98,96,94,.. 2,0
จงเขียนโปรแกรมเพื่อหาค่า 1+2+3+4+ .. 100
จงเขียนโปรแกรมเพื่อหาค่า 1+3+5+7 + 9+ .. 99
จงเขียนโปรแกรมเพื่อหาค่า 2+4+6+8+ .. 100
จงหาผลรวมของตัวเลขที่หารด้วย 5 ลงตัวตั้งแต่ 1-100
Exercise 3 ต่อ
9.
10.
11.
12.
13.
จงเขียนโปรแกรมเพื่อหาค่าเลขจานวนเฉพาะ (Prime
Number )
จงเขียนโปรแกรมเพื่อหาว่า 1-100 นั้นมี Prime
Number กี่ตวั
จงเขียนโปรแกรมเพื่อหาค่า 12 + 22 + 32 + 102
จงเขียนโปรแกรมเพื่อหาค่า 11 + 22 + 33 + 1010
จงเขียนโปรแกรมเพื่อหาค่า 21 + 22 + 23 + 210
Exercise 3 ต่อ
13.
14.
15.
จงเขียนโปรแกรมเพื่อหาค่า 101 + 92 + 83 +.. 110
จงเขียนโปรแกรมเพื่อหาค่า 110 + 29 + 34 +.. 101
จงเขียนโปรแกรมเพื่อหาค่า 210 + 48 + 66 +.. 102
Do Statements

ประโยค do ใช้ควบคุมให้เกิดการทาซ้ า คล้ายประโยค while
แต่ต่างกันตรงที่ เงื่อนไขการหยุดถูกคานวณหลังจากทาประโยค
do แล้ว โดยมีโครงสร้างดังนี้
do {
statements
<statement>;
} while (<boolean expression>);
true
expression
false
ต.ย. การใช้งาน do Statements
//Do1.java
2. class Do1 {
3. public static void main(String args[]) {
4. int n = Integer.parseInt(args[0]);
5. int i=1,f=1;
6. do {
7. f *=1;
8. }
9. while (i++ < n);
10. System.out.println( n + “! : ” + f);
11. }
12. }
1.
Exercise 4
4.1 วิธีในการหาค่าตัวหารร่ วมมาก (Greatest common
Divisor ) ของ Euclid กล่าวไว้ดงั นี้ ถ้ามีอินพุตเป็ น m
และ n ซึ่งเป็ นเลขจานวนเต็มใด ๆ มากกว่าศูนย์
1. ให้ r เป็ นเศษที่เหลือจากการหาร m ด้วย n
2. ถ้า r เป็ นศูนย์ n คือคาตอบ
3. กาหนดให้ m=n และ n=r แล้วกลับไปทาซ้ าในขั้นตอนที่ 1
จงแสดงโปรแกรมสาหรับการหาค่าตัวหารร่ วมมากตามวิธีขา้ งบนนี้
Exercise 4 (ต่อ)
4.2 จงสร้างโปรแกรมสาหรับตรวจสอบว่า
สาหรับ 1 <= n <= 100
n
1
n


n 1
i 0 i (i  1)
4.3 จงเขียนโปรแกรมสาหรับค่าของ pi ให้ได้ค่า
ถูกต้องถึงทศนิยม 7 หลักคือ 3.1415926
1 1
3 5
1
7
1
9
  4 x(1      ..........)
Exercise 4 (ต่อ)
4.4 เลข 18 – 142 เป็ นเลขต่อเนื่องที่บวกกันได้ 10000 พอดี จง
เขียนโปรแกรมเพื่อตรวจสอบว่ามีเลขลาดับต่อเนื่องอื่นที่เป็ นอย่างนี้
หรื อไม่ ถ้ามี มีกี่ชุดและมีค่าจากเท่าใดถึงเท่าใด
For Statements

ประโยค for ถูกแนะนาโดยภาษา Fortran (เรี ยกว่าประโยค do ) ใช้
ในการทาซ้ าที่มีตวั แปรสาหรับนับรอบเรี ยกว่า index เหมาะกับการทาซ้ าที่
ทราบล่วงหน้าว่าจะทากี่ครั้ง แม้วา่ ภายหลังภาษา Fortran จะมีประโยค
while เข้ามาเพิ่ม แต่ประโยค do ก็ยงั มีใช้อยู่ ด้วยเหตุที่ภาษา
Fortran เน้นเรื่ องความเร็ วของโปรแกรมเป็ นอย่างมาก คอมไพล์เลอร์
ภาษา Fortran จึงจัดการกับประโยค do ต่างจากประโยค while เช่น
มีขอ้ จากัดว่า ตัวแปรที่เป็ นตัวนับจะต้องเป็ นเลขจานวนเต็มและห้ามเป็ น
parameter เพื่อให้สามารถเก็บค่าใน register และเปลี่ยนแปลงค่า
ได้อย่างรวดเร็ ว รวมทั้งโปรแกรมจะไม่กอ็ ปปี ค่าของ register กลับไปที่
ตัวแปรที่เป็ นตัวนับ ดังนั้นค่าของตัวนับจะน้อยกว่าค่าจริ งของมันเมื่อจบ
loop แล้ว
For Statements (ต่อ)

ภาษา Pascal , C , C++ และ Java รับประโยค do
ของ Fortran มาดัดแปลงเป็ นประโยค for เพื่อใช้ในการทาซ้ า
ที่มีตวั นับ แต่ไม่ได้นาข้อบังคับดังกล่าวมาด้วย ประโยค for
สาหรับภาษาเหล่านี้เพียงอีกรู ปแบบหนึ่งของประโยค while
เท่านั้น โดยโครงสร้างของประโยค for มีดงั นี้
for (<initial exp> ; <condition exp> ; <update exp>)
<statements>;
ต.ย. การใช้งานประโยค for
1.
2.
3.
4.
5.
6.
7.
8.
9.
//For1.java
class For1 {
public static void main(String args[]) {
int i,f,n=Integer.parseInt(args[0]);
for(i=1,f=1;I <=n; i++)
f*=1;
System.out.println(n + “! : ” + f);
}
}